/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*******************************************************************************
 * Filename:
 * ---------
 *  mdi_camera.c
 *
 * Project:
 * --------
 *  MAUI
 *
 * Description:
 * ------------
 *  Interface to access camera related functions.
 *
 * Author:
 * -------
 * -------
 * -------
 *
 *==============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!! 
 *------------------------------------------------------------------------------
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!! 
 *==============================================================================
 *******************************************************************************/
/***************************************************************************** 
* Include
*****************************************************************************/
#include "MMI_features.h"
#if defined(__MMI_CAMERA__) || defined(__MMI_CAMCORDER__)

#include "stdC.h"
#include "mmi_platform.h"
#include "mmi_frm_gprot.h"
#include "fat_fs.h"
#include "gdi_include.h"        /* include data type */
#include "gui.h"
#include "med_api.h"
#include "mdi_datatype.h"
#include "mdi_include.h"
#include "med_api.h"    /* media task */
#include "mdi_resdef.h"
#include "GlobalDefs.h"

#ifdef __SW_JPEG_CODEC_SUPPORT__
#include "jpeg.h"
#endif

#include "mdi_camera.h" /* include camera related define */
#include "gpioInc.h"    /* avoid sleep when using camera */
#include "fat_fs.h"

#include "lcd_if.h"
#include "lcd_sw_rnd.h"

#include "med_api.h"
#include "med_main.h"
#include "med_struct.h"
#include "FileManagerDef.h"
#if defined(ISP_SUPPORT)
   /* for visual effect */
#ifdef __MTK_TARGET__
#include "reg_base.h"
#include "IntrCtrl.h"
#include "drv_comm.h"
#include "drvpdn.h"
#include "jpeg.h"
#include "visualhisr.h"
#include "visual_comm.h"
#include "fat_fs.h"
#include "isp_if.h"
#include "drv_comm.h"

#if defined(DRV_IDP_OLD_DESIGN)
#include "resizer.h"
#include "imgproc.h"
#include "img_comm.h"
#include "imgdma.h"
#include "Image_effect.h"
#else
#include "image_effect_post.h"
#endif

#endif /* __MTK_TARGET__ */ /* ISP_SUPPORT */
#endif /* defined(ISP_SUPPORT) */ 

#ifdef __MMI_TVOUT__
#include "mdi_tv.h"
#endif 
#include "mmi_trc.h"            /* Debug Info */
/***************************************************************************** 
* Typedef
*****************************************************************************/
/* camera state machine */
typedef enum
{
    CAMERA_STATE_POWER_OFF = 0,
    CAMERA_STATE_IDLE,
    CAMERA_STATE_PREVIEWING,
    CAMERA_STATE_BARCODE_CAPTURING,
    CAMERA_STATE_BCR_READY,
    CAMERA_STATE_BCR_RECOGNIZING,
    CAMERA_STATE_STITCH_PANORAMA
} mdi_camera_state_enum;


#define MDI_CAMERA_CHECK_FOCUS(_X_)\
do{\
    if (camera_setting_p->af_zone##_X_##_x == 0 && camera_setting_p->af_zone##_X_##_y == 0)\
    {\
        cam_preview_data->af_zone##_X_##_x = layer_offset_x + cam_preview_data->preview_width/2;\
        cam_preview_data->af_zone##_X_##_y = layer_offset_y + cam_preview_data->preview_height/2;\
    }\
    else\
    {\
        cam_preview_data->af_zone##_X_##_x = camera_setting_p->af_zone##_X_##_x;\
        cam_preview_data->af_zone##_X_##_y = camera_setting_p->af_zone##_X_##_y;\
    }\
}while(0)

/***************************************************************************** 
* Local Variable
*****************************************************************************/
mdi_camera_context_struct g_mdi_camera_cntx;
mdi_camera_context_struct *mdi_camera_p = &g_mdi_camera_cntx;
mdi_camera_setting_struct *camera_setting_p = &g_mdi_camera_cntx.setting;
mdi_camera_preview_struct *camera_preview_p = &g_mdi_camera_cntx.preview;
mdi_camera_overlay_struct *camera_overlay_p = NULL;
mdi_camera_mwb_process_struct *camera_mwb_p = &g_mdi_camera_cntx.mwb;

static mdi_camera_panorama_callback g_mdi_camera_panorama_callback;

static mdi_camera_af_ind_callback g_mdi_camera_af_callback;
#if defined(__MDI_CAMERA_FD_SUPPORT__)
static mdi_camera_fd_callback g_mdi_camera_fd_callback;
#endif
/* callback function */
void (*mdi_camera_non_block_burst_shot_result_callback) (MDI_RESULT, U16) = NULL;

#ifdef __MDI_CAMERA_XENON_FLASH_SUPPORT__
    void (*mdi_xenon_flash_status_callback) (MDI_RESULT) = NULL;
#endif

/***************************************************************************** 
* Local Function
*****************************************************************************/
static MDI_RESULT mdi_camera_preview_start_internal(void);
static void mdi_camera_af_finish_ind(void *inMsg);
static void mdi_camera_panorama_result_ind(void *inMsg);


#if defined(BARCODE_SUPPORT)
static MDI_RESULT mdi_camera_barcodereader_preview_start_internal(void);
static mdi_barcode_callback barcodeCurrentHandler;
static void mdi_camera_barcode_decode_finish_ind(void *inMsg);
static MDI_RESULT mdi_camera_barcodereader_preview_start_internal(void);
#endif

#if defined(BCR_SUPPORT)
static mdi_bcr_callback BcrCurrentHandler;
#endif


#if MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES
static void mdi_camera_non_block_burst_capture_result_hdlr(void *msg_ptr);
static void mmi_camera_fill_overlay_data(media_cam_capture_req_struct* capture_data);
#endif 
#if defined(__MDI_CAMERA_FD_SUPPORT__)
static void mdi_camera_fd_result_ind(void *inMsg);
#endif

/***************************************************************************** 
* External Function
*****************************************************************************/
#if MDI_CAMERA_MT6238_SERIES || (defined(MT6253) || defined(MT6253T) || defined(MT6235) || defined(MT6235B))
extern CAP_SIZE camera_iso_imgsize_upperbound(CAM_SCENE_MODE iso_value);
extern void camera_capability_func_config(camera_capability_func_struct** camera_capability_func);
#endif

#ifdef __MDI_CAMERA_XENON_FLASH_SUPPORT__
static void mdi_camera_xenon_flash_ind(void *inMsg);
/*****************************************************************************
 * FUNCTION
 *  mdi_camera_xenon_flash_ind
 * DESCRIPTION
 *  
 * PARAMETERS
 *  inMsg       [?]     
 * RETURNS
 *  void
 *****************************************************************************/
static void mdi_camera_xenon_flash_ind(void *inMsg)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_xenon_flash_ind_struct *msg_p = (media_cam_xenon_flash_ind_struct*) inMsg;
    U16 status = msg_p->status;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    MMI_TRACE(MMI_MEDIA_TRC_G4_MDI,MDI_TRC_CAMERA_XENON_FLASH_STATUS,status);
    
    if (mdi_xenon_flash_status_callback)
    {   
        switch (status)
        {
            case CAM_XENON_FLASH_READY:
                mdi_xenon_flash_status_callback(MDI_RES_CAMERA_SUCCEED);
                break;
            case CAM_XENON_FLASH_CHARGING:
                mdi_xenon_flash_status_callback(MDI_RES_CAMERA_XENONFLASH_CHARGING);
                break;
            case CAM_XENON_FLASH_TIMEOUT:
                mdi_xenon_flash_status_callback(MDI_RES_CAMERA_ERR_XENONFLASH_TIMEOUT);
                break;
            case CAM_XENON_FLASH_LOW_BATTERY:
                mdi_xenon_flash_status_callback(MDI_RES_CAMERA_ERR_XENONFLASH_LOW_BATTERY);
                break;
        }
    }
#endif /* __MTK_TARGET__ */ 
}
#endif /* __MDI_CAMERA_XENON_FLASH_SUPPORT__ */


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_error_info
 * DESCRIPTION
 *  Get the error string and error type
 * PARAMETERS
 *  error_code   [IN]     Error code got from MDI APIs
 *  popup_type   [OUT]    Popup_type of this error_code 
 * RETURNS
 *  string_id
 *****************************************************************************/
MMI_ID_TYPE mdi_camera_get_error_info(MDI_RESULT error_code, mmi_event_notify_enum* popup_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_ID_TYPE string_id = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    *popup_type = MMI_EVENT_FAILURE;
    switch (error_code)
    {
        case MDI_RES_CAMERA_ERR_FAILED:
            string_id = STR_GLOBAL_ERROR;
            break;
        case MDI_RES_CAMERA_ERR_DISK_FULL:
            string_id = FMGR_FS_DISK_FULL_TEXT;
            break;
        case MDI_RES_CAMERA_ERR_WRITE_PROTECTION:
            string_id = STR_ID_MDI_CAMERA_WRITE_PROTECTION;
            break;
        case MDI_RES_CAMERA_ERR_NO_DISK:
            string_id = FMGR_FS_DRIVE_NOT_FOUND_TEXT;
            break;             
        case MDI_RES_CAMERA_ERR_HW_NOT_READY:
            string_id = STR_ID_MDI_CAMERA_HW_NOT_READY;
            break;
        case MDI_RES_CAMERA_ERR_MEMORY_NOT_ENOUGH:
            string_id = STR_GLOBAL_NOT_ENOUGH_MEMORY;
            break;
        case MDI_RES_CAMERA_ERR_PREVIEW_FAILED:
            string_id = STR_ID_MDI_CAMERA_PREVIEW_FAILED;
            break;
        case MDI_RES_CAMERA_ERR_CAPTURE_FAILED:
            string_id = STR_ID_MDI_CAMERA_CAPTURE_FAILED;
            break;
        case MDI_RES_CAMERA_ERR_ROOT_DIR_FULL:
            string_id = FMGR_FS_ROOT_DIR_FULL_TEXT;
            break;
        case MDI_RES_CAMERA_ERR_XENONFLASH_TIMEOUT:
            string_id = STR_ID_MDI_CAMERA_XENONFLASH_TIMEOUT;
            break;
        case MDI_RES_CAMERA_ERR_XENONFLASH_LOW_BATTERY:
            string_id = STR_ID_MDI_CAMERA_XENONFLASH_LOW_BATTERY;
            break;
        case MDI_RES_BARCODEREADER_IMAGE_ERROR:
        case MDI_RES_BARCODEREADER_MODULE_ERROR:
        case MDI_RES_BARCODEREADER_OVER_VERSION:
        case MDI_RES_BARCODEREADER_OUT_OF_MEMORY:
        case MDI_RES_BARCODEREADER_ERROR_CODE_TYPE:
            string_id = STR_ID_MDI_BARCODEREADER_DECODE_FAILED;
            break;           
        case MDI_RES_BARCODEREADER_DECODE_TIMEOUT:
            string_id = STR_ID_MDI_BARCODEREADER_DECODE_TIMEOUT;
            break;
            
        default:
            string_id = STR_GLOBAL_ERROR;
            break;
    }
    return string_id;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_init
 * DESCRIPTION
 *  Init MDI camera
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED  Success
 *****************************************************************************/
MDI_RESULT mdi_camera_init(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    mdi_camera_p->state = CAMERA_STATE_POWER_OFF;
    mdi_camera_p->seq_num = 0;
    mdi_camera_p->stitch_seq_num = 0;
    mdi_camera_p->burst_cap_count = 0;
    #if defined(__MDI_CAMERA_HW_ROTATOR_SUPPORT__)
    mdi_camera_p->hw_rotate = MDI_CAMERA_PREVIEW_ROTATE_0;
    #endif
    mdi_camera_p->cam_id = MDI_CAMERA_MAIN;
    return MDI_RES_CAMERA_SUCCEED;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_set_hw_rotator 
 * DESCRIPTION
 *  Set HW rotation value for replacing LCD controller rotating with IDP/LCD rotator
 * PARAMETERS
 *  rotate      [IN]        HW rotation value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_set_hw_rotator(U16 rotate)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if defined(__MDI_CAMERA_HW_ROTATOR_SUPPORT__)
    mdi_camera_p->hw_rotate = rotate;
#endif
}

/*****************************************************************************
 * FUNCTION
 *  mdi_camera_detect_sensor_id
 * DESCRIPTION
 *  Detect sensor module id
 * PARAMETERS
 *  cam_id          [IN]        Main/sub
 * RETURNS
 *  MDI_RES_WEBCAM_SUCCEED      Success
 *  MDI_RES_WEBCAM_ERR_FAILED   Failure
 *****************************************************************************/
MDI_RESULT mdi_camera_detect_sensor_id(U8 cam_id)
{
#if defined(__MTK_TARGET__) && defined(ISP_SUPPORT)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 ret;
    media_cam_detect_sensor_id_req_struct detect_req;
        
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    detect_req.cam_id = cam_id;
    
    ret = media_cam_detect_sensor_id(stack_int_get_active_module_id(), &detect_req);
    if (ret == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_FAILED;
    }
#else
    return MDI_RES_CAMERA_SUCCEED;
#endif /* defined(__MTK_TARGET__) && defined(ISP_SUPPORT) */
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_power_on
 * DESCRIPTION
 *  Power on the camera module
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_WEBCAM_SUCCEED      Success
 *  MDI_RES_WEBCAM_ERR_FAILED   Failure
 *****************************************************************************/
MDI_RESULT mdi_camera_power_on()
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 ret;
    media_cam_power_up_req_struct cam_powerup_data;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* check if state is right */
    ASSERT(mdi_camera_p->state == CAMERA_STATE_POWER_OFF);

    /* it is for MT6238 only */
    ASSERT(camera_overlay_p == NULL);

#ifdef __MDI_CAMERA_HW_ROTATOR_SUPPORT__
    mdi_camera_p->hw_rotate = MDI_CAMERA_PREVIEW_ROTATE_0;
#endif

    cam_powerup_data.seq_num = mdi_camera_p->seq_num;
    cam_powerup_data.cam_id = mdi_camera_p->cam_id;

    ret = mdi_camera_detect_sensor_id(mdi_camera_p->cam_id);

    if (ret != MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }

    ret = media_cam_power_up(stack_int_get_active_module_id(), (void*)&cam_powerup_data);

    if (ret == MED_RES_OK)
    {
        /* Power On succeed */
        mdi_camera_p->state = CAMERA_STATE_IDLE;
        return MDI_RES_CAMERA_SUCCEED;
    }
    else
    {
        mdi_camera_p->cam_id = MDI_CAMERA_MAIN;
        return MDI_RES_CAMERA_ERR_FAILED;
    }

#else /* __MTK_TARGET__ */ 
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* avoid compile warning in MNT */
    return MDI_RES_CAMERA_SUCCEED;
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_power_off
 * DESCRIPTION
 *  Power off the camera module
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_WEBCAM_SUCCEED      Success
 *****************************************************************************/
MDI_RESULT mdi_camera_power_off(void)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    media_cam_power_down(stack_int_get_active_module_id(), 100);

    mdi_camera_p->state = CAMERA_STATE_POWER_OFF;
    mdi_camera_p->seq_num++;

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    if (camera_overlay_p!=NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif
    mdi_camera_p->cam_id = MDI_CAMERA_MAIN;
    return MDI_RES_CAMERA_SUCCEED;

#else /* __MTK_TARGET__ */ 
    /* avoid compile warning in MNT */
    mdi_camera_p->cam_id = MDI_CAMERA_MAIN;
    mdi_camera_p->state = CAMERA_STATE_POWER_OFF;
    return MDI_RES_CAMERA_SUCCEED;
#endif /* __MTK_TARGET__ */ 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_preview_start
 * DESCRIPTION
 *  Start camera preview
 * PARAMETERS
 *  preview_p                   [IN]        Preview data
 *  setting_p                   [IN]        Setting data
 * RETURNS
 *  MDI_RES_WEBCAM_SUCCEED      Success
 *  MDI_RES_WEBCAM_ERR_FAILED   Failure
 *****************************************************************************/
MDI_RESULT mdi_camera_preview_start(
            mdi_camera_preview_struct *preview_p,
            mdi_camera_setting_struct *setting_p)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 ret;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* arg check */
    ASSERT(preview_p->blt_layer_flag > 0);
    ASSERT(preview_p->preview_layer_flag > 0);

    g_mdi_camera_cntx.is_tvout = preview_p->is_tvout;

    /* 
     * Note, src_key_color in camera_preview_p only needs for MT6218 with backend, for MT6218,
     * we only support 16 bit color depth, so, U16 is enoguth 
     */
     
    /* init camera parameters */
    memcpy(camera_preview_p, preview_p, sizeof(mdi_camera_preview_struct));
    memcpy(camera_setting_p, setting_p, sizeof(mdi_camera_setting_struct));

#ifdef __MDI_CAMERA_XENON_FLASH_SUPPORT__
    mdi_xenon_flash_status_callback = setting_p->xenon_flash_status_callback;
    SetProtocolEventHandler(mdi_camera_xenon_flash_ind, MSG_ID_MEDIA_CAM_XENON_FLASH_STATUS_IND);
#endif

    if (mdi_camera_p->state == CAMERA_STATE_IDLE)
    {
        ret = mdi_camera_preview_start_internal();

        if (ret >= 0)
        {
            /* preview successfully */
            mdi_camera_p->state = CAMERA_STATE_PREVIEWING;
        }

        return ret;
    }
    else
    {
        /* start preview at wrong state */
    #ifndef __MTK_TARGET__  /* MNT Simulator */
        /* on MNT, direct call internal function for debugging */
        mdi_camera_preview_start_internal();
        return MDI_RES_CAMERA_SUCCEED;

    #else /* __MTK_TARGET__ */ 
        /* Target */
        return MDI_RES_CAMERA_ERR_FAILED;
    #endif /* __MTK_TARGET__ */ 
    }
}


static void mdi_camera_fillin_preview_data(media_cam_preview_req_struct* cam_preview_data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    U8 *buf_ptr;
    S32 layer_width;
    S32 layer_height;
    S32 layer_offset_x;
    S32 layer_offset_y;
    U8 layer_rotate;    
    S32 lcd_width;
    S32 lcd_height;
    S32 lcd_offset_x;
    S32 lcd_offset_y;
    S32 lcd_start_x;
    S32 lcd_start_y;
    S32 lcd_end_x;
    S32 lcd_end_y;
    S32 clip_x1,clip_x2,clip_y1,clip_y2;
#ifdef __MMI_TVOUT__
    MDI_RESULT ret;
#endif
    U8 cur_lcd_rotate;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* if preview on sublcd, set sublcd active, so we may get correct paramater from following functions */
    if (camera_setting_p->lcm == MDI_CAMERA_PREVIEW_LCM_SUBLCD)
    {
        gdi_lcd_set_active(GDI_LCD_SUB_LCD_HANDLE);
    }

    cur_lcd_rotate = gdi_lcd_get_rotate();

#if (defined(MT6253) || defined(MT6253T)) && defined(__MDI_CAMERA_HW_ROTATOR_SUPPORT__)
    if (mdi_camera_p->hw_rotate == MDI_CAMERA_PREVIEW_ROTATE_90)
    {
        camera_setting_p->lcm_rotate = MDI_CAMERA_LCM_ROTATE_270;
    }
    else if (mdi_camera_p->hw_rotate == MDI_CAMERA_PREVIEW_ROTATE_90_MIRROR)
    {
        camera_setting_p->lcm_rotate = MDI_CAMERA_LCM_ROTATE_270_MIRROR;
    }
    else if (mdi_camera_p->hw_rotate == MDI_CAMERA_PREVIEW_ROTATE_270)
    {
        camera_setting_p->lcm_rotate = MDI_CAMERA_LCM_ROTATE_90;
    }
    else if (mdi_camera_p->hw_rotate == MDI_CAMERA_PREVIEW_ROTATE_270_MIRROR)
    {
        camera_setting_p->lcm_rotate = MDI_CAMERA_LCM_ROTATE_90_MIRROR;
    }
#endif /* (defined(MT6253) || defined(MT6253T)) && defined(__MDI_CAMERA_HW_ROTATOR_SUPPORT__) */
    /* get layer info */
    gdi_layer_push_and_set_active(camera_preview_p->preview_layer_handle);
    gdi_layer_get_buffer_ptr(&buf_ptr);
    gdi_layer_get_dimension(&layer_width, &layer_height);
    gdi_layer_get_position(&layer_offset_x, &layer_offset_y);
    gdi_layer_get_clip(&clip_x1,&clip_y1,&clip_x2,&clip_y2);
    layer_rotate = gdi_layer_get_rotate();
    gdi_layer_pop_and_restore_active();

    if (camera_setting_p->lcm == MDI_CAMERA_PREVIEW_LCM_SUBLCD)
    {
        gdi_lcd_set_active(GDI_LCD_MAIN_LCD_HANDLE);
    }

    /* preview on whihc lcm */
    if (camera_setting_p->lcm == MDI_CAMERA_PREVIEW_LCM_MAINLCD)
    {
        cam_preview_data->lcd_id = MAIN_LCD;
        lcd_width = LCD_WIDTH;
        lcd_height = LCD_HEIGHT;
    }
    else
    {
        cam_preview_data->lcd_id = SUB_LCD;
        gdi_lcd_set_active(GDI_LCD_SUB_LCD_HANDLE);
        gdi_lcd_get_dimension(&lcd_width, &lcd_height);
        gdi_lcd_set_active(GDI_LCD_MAIN_LCD_HANDLE);
    }

    cam_preview_data->camera_offset_x = (layer_offset_x < 0) ? 0 : layer_offset_x;
    cam_preview_data->camera_offset_y = (layer_offset_y < 0) ? 0 : layer_offset_y;

    cam_preview_data->preview_width = camera_setting_p->preview_width;
    cam_preview_data->preview_height = camera_setting_p->preview_height;

    if ((camera_setting_p->lcm_rotate == MDI_CAMERA_LCM_ROTATE_270) ||
        (camera_setting_p->lcm_rotate == MDI_CAMERA_LCM_ROTATE_270_MIRROR))
    {
        lcd_offset_x = GDI_LCD_WIDTH - layer_offset_y - clip_y2 - 1;
        lcd_offset_y = layer_offset_x + clip_x1;
    }
    else if ((camera_setting_p->lcm_rotate == MDI_CAMERA_LCM_ROTATE_90) ||
             (camera_setting_p->lcm_rotate == MDI_CAMERA_LCM_ROTATE_90_MIRROR))
    {
        lcd_offset_x = layer_offset_y + clip_y1;
        lcd_offset_y = GDI_LCD_HEIGHT - layer_offset_x - clip_x2 - 1;
    }
    else
    {
    #if defined(GDI_USING_LAYER_ROTATE)
        if (layer_rotate == LCD_LAYER_ROTATE_90)
        {
            lcd_offset_x = GDI_LCD_WIDTH - layer_offset_y - clip_y2;
            lcd_offset_y = layer_offset_x + clip_x1;
        }
        else
    #endif /* defined(GDI_USING_LAYER_ROTATE) */ 
        {
            lcd_offset_x = layer_offset_x + clip_x1;
            lcd_offset_y = layer_offset_y + clip_y1;
        }        
    }

    lcd_start_x = lcd_offset_x;
    lcd_start_y = lcd_offset_y;

    if ((camera_setting_p->lcm_rotate == MDI_CAMERA_LCM_ROTATE_270) ||
        (camera_setting_p->lcm_rotate == MDI_CAMERA_LCM_ROTATE_90) ||
        (camera_setting_p->lcm_rotate == MDI_CAMERA_LCM_ROTATE_270_MIRROR) ||
        (camera_setting_p->lcm_rotate == MDI_CAMERA_LCM_ROTATE_90_MIRROR))
    {
        /* rotate 90 or 270 for horizonal view */
        lcd_end_x = lcd_offset_x + (clip_y2 - clip_y1);
        lcd_end_y = lcd_offset_y + (clip_x2 - clip_x1);
    }
    else
    {
        lcd_end_x = lcd_offset_x + (clip_x2 - clip_x1);
        lcd_end_y = lcd_offset_y + (clip_y2 - clip_y1);
    }

    /* force inside lcd region */
    if (lcd_start_x < 0)
    {
        lcd_start_x = 0;
    }
    else if (lcd_start_x > lcd_width - 1)
    {
        lcd_start_x = lcd_width - 1;
    }

    if (lcd_start_y < 0)
    {
        lcd_start_y = 0;
    }
    else if (lcd_start_y > lcd_height - 1)
    {
        lcd_start_y = lcd_height - 1;
    }

    if (lcd_end_x < 0)
    {
        lcd_end_x = 0;
    }
    else if (lcd_end_x > lcd_width - 1)
    {
        lcd_end_x = lcd_width - 1;
    }

    if (lcd_end_y < 0)
    {
        lcd_end_y = 0;
    }
    else if (lcd_end_y > lcd_height - 1)
    {
        lcd_end_y = lcd_height - 1;
    }

    if (gdi_lcd_get_rotate_by_layer())
    {
        /* if using LCD rotate, the roi region will be the phisical potion as lcd */
        cam_preview_data->roi_offset_x = GDI_LAYER_OFFSET + lcd_offset_x;
        cam_preview_data->roi_offset_y = GDI_LAYER_OFFSET + lcd_offset_y;
    }
    else
    {
        /* if using LCM rotate, layers are rotated, so the position is the same as layer pos */
        cam_preview_data->roi_offset_x = GDI_LAYER_OFFSET + layer_offset_x + clip_x1;
        cam_preview_data->roi_offset_y = GDI_LAYER_OFFSET + layer_offset_y + clip_y1;
    }

    /* force inside lcd region */
    if (cam_preview_data->roi_offset_x < GDI_LAYER_OFFSET)
    {
        cam_preview_data->roi_offset_x = GDI_LAYER_OFFSET;
    }
    else if (cam_preview_data->roi_offset_x > GDI_LAYER_OFFSET + UI_device_width - 1)
    {
        cam_preview_data->roi_offset_x = GDI_LAYER_OFFSET + UI_device_width - 1;
    }

    if (cam_preview_data->roi_offset_y < GDI_LAYER_OFFSET)
    {
        cam_preview_data->roi_offset_y = GDI_LAYER_OFFSET;
    }
    else if (cam_preview_data->roi_offset_y > GDI_LAYER_OFFSET + UI_device_height - 1)
    {
        cam_preview_data->roi_offset_y = GDI_LAYER_OFFSET + UI_device_height - 1;
    }

    cam_preview_data->lcd_start_x = lcd_start_x;
    cam_preview_data->lcd_start_y = lcd_start_y;
    cam_preview_data->lcd_end_x = lcd_end_x;
    cam_preview_data->lcd_end_y = lcd_end_y;   
    cam_preview_data->lcd_update = camera_preview_p->is_lcd_update;

    /* preview offset x, y are offset from lcd_start_x, lcd_start_y */
    cam_preview_data->preview_offset_x = 0; /* not used */
    cam_preview_data->preview_offset_y = 0; /* not used */

    cam_preview_data->image_buffer_p = (void*)buf_ptr;
    cam_preview_data->image_width = (kal_uint16) camera_setting_p->image_width;
    cam_preview_data->image_height = (kal_uint16) camera_setting_p->image_height;

    cam_preview_data->buffer_width = (kal_uint16) layer_width;
    cam_preview_data->buffer_height = (kal_uint16) layer_height;
    cam_preview_data->image_buffer_size =
        (kal_uint32) ((camera_setting_p->preview_width * camera_setting_p->preview_height *
                       GDI_LAYER.act_bit_per_pixel) >> 3);

    /* set settings parameters */
    cam_preview_data->effect = (kal_uint16) camera_setting_p->effect;
    cam_preview_data->WB = (kal_uint16) camera_setting_p->wb;
    cam_preview_data->exposure = (kal_uint16) camera_setting_p->ev;
    cam_preview_data->zoom_factor = (kal_uint16) camera_setting_p->zoom;
    cam_preview_data->banding_freq = (kal_uint8) camera_setting_p->banding;
    cam_preview_data->flash_mode = (kal_uint16) camera_setting_p->flash;

    cam_preview_data->brightness = (kal_uint16) camera_setting_p->brightness;
    cam_preview_data->contrast = (kal_uint16) camera_setting_p->contrast;
    cam_preview_data->saturation = (kal_uint16) camera_setting_p->saturation;
    cam_preview_data->hue = (kal_uint16) camera_setting_p->hue;
    cam_preview_data->night_mode = (kal_bool) camera_setting_p->night;
    cam_preview_data->snapshot_number = 1;

    cam_preview_data->continue_capture = (kal_uint8) camera_setting_p->continue_capture;

#if ( MDI_CAMERA_MT6225_SERIES || MDI_CAMERA_MT6227_SERIES)
    cam_preview_data->ae_metering_mode = (kal_uint8) camera_setting_p->ae_meter;
    cam_preview_data->dsc_mode = (kal_uint8) camera_setting_p->dsc_mode;
    cam_preview_data->af_metering_mode = (kal_uint8) camera_setting_p->af_metering_mode;
    cam_preview_data->af_mode = (kal_uint8) camera_setting_p->af_mode;
    cam_preview_data->iso_setting = (kal_uint8) camera_setting_p->iso;
#elif (MDI_CAMERA_MT6228_SERIES)
    cam_preview_data->ae_metering_mode = (kal_uint8) camera_setting_p->ae_meter;
    cam_preview_data->iso_setting = (kal_uint8) camera_setting_p->iso;
    cam_preview_data->dsc_mode = (kal_uint8) camera_setting_p->dsc_mode;
    cam_preview_data->tv_setting = (kal_uint16) camera_setting_p->shutter_pri;
    cam_preview_data->av_setting = (kal_uint8) camera_setting_p->aperture_pri;
    cam_preview_data->af_metering_mode = (kal_uint8) camera_setting_p->af_metering_mode;
    cam_preview_data->af_mode = (kal_uint8) camera_setting_p->af_mode;

    cam_preview_data->overlay_frame_mode = camera_setting_p->overlay_frame_mode;
    cam_preview_data->overlay_color_depth = camera_setting_p->overlay_frame_depth;
    cam_preview_data->overlay_frame_source_key = camera_setting_p->overlay_frame_source_key;
    cam_preview_data->overlay_frame_width = camera_setting_p->overlay_frame_width;
    cam_preview_data->overlay_frame_height = camera_setting_p->overlay_frame_height;
    cam_preview_data->overlay_frame_buffer_address = camera_setting_p->overlay_frame_buffer_address;
    #ifdef __MTK_TARGET__
    cam_preview_data->image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif
    cam_preview_data->image_pitch_mode = FALSE;
    cam_preview_data->tv_output = FALSE;
#elif (MDI_CAMERA_MT6238_SERIES)
    /* ISP Setting */
    cam_preview_data->ae_metering_mode = (kal_uint8) camera_setting_p->ae_meter;
    cam_preview_data->af_operation_mode = (kal_uint16) camera_setting_p->af_operation_mode;
    cam_preview_data->af_range = (kal_uint8) camera_setting_p->af_range;
    cam_preview_data->sharpness_level = (kal_uint8) camera_setting_p->sharpness;
    cam_preview_data->preview_rotate_value = (kal_uint8) camera_setting_p->preview_rotate;
    cam_preview_data->binning_mode = MMI_FALSE;

    cam_preview_data->max_zoom_factor = camera_setting_p->max_zoom_factor = 800;
    cam_preview_data->total_zoom_step = camera_setting_p->total_zoom_step = 40;

    /*
     *  To switch ISO mode to DSC mode.
     *  When ISO >= 800, need to enable binning mode.
     */
    if (camera_setting_p->iso == MDI_CAMERA_ISO_AUTO)
    {
        cam_preview_data->dsc_mode = (kal_uint8) camera_setting_p->dsc_mode;

    }
    else
    {
        cam_preview_data->dsc_mode = (kal_uint8) camera_setting_p->iso;

        if (cam_preview_data->dsc_mode == MDI_CAMERA_ISO_800 || cam_preview_data->dsc_mode == MDI_CAMERA_ISO_1600 )
            cam_preview_data->binning_mode = MMI_TRUE;
    }
    /* lcd rotate & layer rotate should be 0 */
#if defined(__MDI_CAMERA_HW_ROTATOR_SUPPORT__)
    if (mdi_camera_p->hw_rotate != MDI_CAMERA_PREVIEW_ROTATE_0)
    {
        ASSERT(cur_lcd_rotate == 0 && layer_rotate == 0);
    }

    switch (mdi_camera_p->hw_rotate)
    {
        case MDI_CAMERA_PREVIEW_ROTATE_0:
            cam_preview_data->rot_angle = IMG_ROT_ANGLE_0;
            break;
        case MDI_CAMERA_PREVIEW_ROTATE_90:
            cam_preview_data->rot_angle = IMG_ROT_ANGLE_90;
            break;
        case MDI_CAMERA_PREVIEW_ROTATE_180:
            cam_preview_data->rot_angle = IMG_ROT_ANGLE_180;
            break;
        case MDI_CAMERA_PREVIEW_ROTATE_270:
            cam_preview_data->rot_angle = IMG_ROT_ANGLE_270;
            break;
    }
#endif /* defined(__MDI_CAMERA_HW_ROTATOR_SUPPORT__) */
    /* IDP Setting */
    cam_preview_data->image_data_format = IMG_COLOR_FMT_RGB565;
    cam_preview_data->image_pitch_mode = FALSE;
    cam_preview_data->image_bg_image_width = camera_setting_p->preview_width;            /* pitch width */

    cam_preview_data->overlay_frame_mode = camera_setting_p->overlay_frame_mode;
    cam_preview_data->overlay_color_depth = camera_setting_p->overlay_frame_depth;
    cam_preview_data->overlay_frame_source_key = camera_setting_p->overlay_frame_source_key;
    cam_preview_data->overlay_frame_width = camera_setting_p->overlay_frame_width;
    cam_preview_data->overlay_frame_height = camera_setting_p->overlay_frame_height;
    cam_preview_data->overlay_frame_buffer_address = camera_setting_p->overlay_frame_buffer_address;

    cam_preview_data->overlay_palette_size = camera_setting_p->overlay_palette_size;
    cam_preview_data->overlay_palette_addr = camera_setting_p->overlay_palette_addr;
#endif 

    MDI_CAMERA_CHECK_FOCUS(0);
    MDI_CAMERA_CHECK_FOCUS(1);
    MDI_CAMERA_CHECK_FOCUS(2);
    MDI_CAMERA_CHECK_FOCUS(3);
    MDI_CAMERA_CHECK_FOCUS(4);

    /* rotate */
    cam_preview_data->rotate = camera_setting_p->preview_rotate;
    cam_preview_data->lcd_rotate = camera_setting_p->lcm_rotate;

    /* fixed para */
    cam_preview_data->gamma = 0;
    cam_preview_data->frame_rate = CAMERA_PERVIEW_FRAME_RATE;

    cam_preview_data->update_layer = camera_preview_p->blt_layer_flag;
    cam_preview_data->hw_update_layer = camera_preview_p->preview_layer_flag;
    cam_preview_data->src_key_color = camera_preview_p->src_key_color;   /* GDI_COLOR_TRANSPARENT */
    cam_preview_data->roi_background_color = GDI_COLOR_BLACK;

#if defined(__MDI_CAMERA_FD_SUPPORT__)
    cam_preview_data->fd_enable = camera_setting_p->fd_enable;
#endif


#if (MDI_CAMERA_MT6228_SERIES||MDI_CAMERA_MT6238_SERIES)
    cam_preview_data->tv_output = FALSE;
#endif 

    /* TV out */
#ifdef __MMI_TVOUT__

    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            /* set tvout owner to Camera */
            g_mdi_camera_cntx.tvout_prev_owner = mdi_tvout_get_owner();

            if (g_mdi_camera_cntx.tvout_prev_owner != MDI_TV_OWNER_CAMERA)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_CAMERA);
            }

            ret = mdi_tvout_get_para(
                    MDI_TV_OWNER_CAMERA,
                    (PU16) & cam_preview_data->tv_output_mode,     /* mode_ptr */
                    (PU16) & cam_preview_data->tv_output_width, /* buf_width_ptr */
                    (PU16) & cam_preview_data->tv_output_height,        /* buf_height_ptr */
                    (PU32) & cam_preview_data->tv_output_buffer_size,   /* buf size */
                    (PU8*) & cam_preview_data->tv_output_buffer1_address,      /* buf1 pp */
                    (PU8*) & cam_preview_data->tv_output_buffer2_address,      /* buf2 pp */
                    NULL,      /* rotate_buf_1_pp */
                    NULL);     /* rotate_buf_2_pp */

            if (ret == MDI_RES_TV_ENABLE)
            {
                cam_preview_data->tv_output = TRUE;

                if (gdi_lcd_get_rotate_by_layer())
                {
                    /* if using LCD rotate, the roi region will be the phisical potion as lcd */
                    cam_preview_data->tv_output_offset_x = (lcd_offset_x < 0) ? 0 : lcd_offset_x;
                    cam_preview_data->tv_output_offset_y = (lcd_offset_y < 0) ? 0 : lcd_offset_y;
                }
                else
                {
                    /* if using LCM rotate, layers are rotated, so the position is the same as layer pos */
                    cam_preview_data->tv_output_offset_x = (layer_offset_x < 0) ? 0 : layer_offset_x;
                    cam_preview_data->tv_output_offset_y = (layer_offset_y < 0) ? 0 : layer_offset_y;
                }
            }
            else
            {
                /* tvout not enable , restore owner to GDI */
                if (g_mdi_camera_cntx.tvout_prev_owner != MDI_TV_OWNER_CAMERA)
                {
                    mdi_tvout_set_owner(g_mdi_camera_cntx.tvout_prev_owner);
                }
            }
        }   /* tvout is enable or not */
    }
#endif /* __MMI_TVOUT__ */ 

#if (MDI_CAMERA_MT6228_SERIES) || (MDI_CAMERA_MT6238_SERIES)
    /* store preview info for MT6228 burst shot */
    g_mdi_camera_cntx.lcd_id = cam_preview_data->lcd_id;
    g_mdi_camera_cntx.lcm_start_x = cam_preview_data->lcd_start_x;
    g_mdi_camera_cntx.lcm_start_y = cam_preview_data->lcd_start_y;
    g_mdi_camera_cntx.lcm_end_x = cam_preview_data->lcd_end_x;
    g_mdi_camera_cntx.lcm_end_y = cam_preview_data->lcd_end_y;
    g_mdi_camera_cntx.update_layer = cam_preview_data->update_layer;
    g_mdi_camera_cntx.hw_update_layer = cam_preview_data->hw_update_layer;
    g_mdi_camera_cntx.frame_buf_size = cam_preview_data->image_buffer_size;
    g_mdi_camera_cntx.frame_buf_ptr = cam_preview_data->image_buffer_p;
    g_mdi_camera_cntx.roi_offset_x = cam_preview_data->roi_offset_x;
    g_mdi_camera_cntx.roi_offset_y = cam_preview_data->roi_offset_y;
    g_mdi_camera_cntx.tv_output_offset_x = cam_preview_data->tv_output_offset_x;
    g_mdi_camera_cntx.tv_output_offset_y = cam_preview_data->tv_output_offset_y;
#endif 
}


#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
    #ifdef __MMI_TVOUT__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
    #endif /* __MMI_TVOUT__ */
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#if ( MDI_CAMERA_MT6225_SERIES || MDI_CAMERA_MT6227_SERIES)
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#elif (MDI_CAMERA_MT6228_SERIES)
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
    #ifdef __MTK_TARGET__
/* under construction !*/
    #endif
/* under construction !*/
/* under construction !*/
#elif (MDI_CAMERA_MT6238_SERIES)
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif 
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#ifdef __MMI_TVOUT__
/* under construction !*/
/* under construction !*/
#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
    #if (MDI_CAMERA_MT6238_SERIES) 
/* under construction !*/
/* under construction !*/
    #endif
/* under construction !*/
#endif /* (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) */ 
/* under construction !*/
/* under construction !*/
#endif /* __MMI_TVOUT__ */ 
/* under construction !*/
#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES)
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif 
/* under construction !*/
#endif /* #if 0 */

/*****************************************************************************
 * FUNCTION
 *  mdi_camera_preview_start_internal
 * DESCRIPTION
 *  init MDI Camera
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RESULT
 *****************************************************************************/
static MDI_RESULT mdi_camera_preview_start_internal(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_preview_req_struct cam_preview_data;
    // under construction 
    // media_cam_preview_req_struct cam_preview_data2;
    MDI_RESULT ret;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* fillin preview data from camera_preview_p and camera_setting_p global vars */
    mdi_camera_fillin_preview_data(&cam_preview_data);

    // under construction 
    //mdi_camera_fillin_preview_data_v2(&cam_preview_data2);

#ifdef __MDI_CAMERA_XENON_FLASH_SUPPORT__
    mdi_xenon_flash_status_callback = camera_setting_p->xenon_flash_status_callback;
    SetProtocolEventHandler(mdi_camera_xenon_flash_ind, MSG_ID_MEDIA_CAM_XENON_FLASH_STATUS_IND);
#endif

#if defined(__MDI_CAMERA_FD_SUPPORT__)
    g_mdi_camera_fd_callback = camera_setting_p->fd_callback;
    SetProtocolEventHandler(mdi_camera_fd_result_ind, MSG_ID_MEDIA_CAM_FD_RESULT_IND);
#endif


    mdi_camera_p->is_preview_stopped = FALSE;

#ifdef __MTK_TARGET__
    ret = media_cam_preview(stack_int_get_active_module_id(), &cam_preview_data);

    if (ret != MED_RES_OK)
    {
        return MDI_RES_CAMERA_ERR_HW_NOT_READY;
    }
    else
    {
        mdi_camera_p->state = CAMERA_STATE_PREVIEWING;
        return MDI_RES_CAMERA_SUCCEED;
    }
#else /* __MTK_TARGET__ */ 
    ret = MED_RES_OK;
    return ret;
#endif /* __MTK_TARGET__ */ 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_preview_stop
 * DESCRIPTION
 *  Stop camera preview
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_WEBCAM_SUCCEED      Success
 *****************************************************************************/
MDI_RESULT mdi_camera_preview_stop(void)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->is_preview_stopped == FALSE)
    {
        media_cam_stop(stack_int_get_active_module_id());
    }

#ifdef __MDI_CAMERA_XENON_FLASH_SUPPORT__
    mdi_xenon_flash_status_callback = NULL;
#endif

#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    /* already powered off */
    if (mdi_camera_p->state == CAMERA_STATE_POWER_OFF)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }

    mdi_camera_p->state = CAMERA_STATE_IDLE;
    mdi_camera_p->is_preview_stopped = TRUE;

    return MDI_RES_CAMERA_SUCCEED;

#else /* __MTK_TARGET__ */ 

    mdi_camera_p->state = CAMERA_STATE_IDLE;
    /* avoid compile warning in MNT */
    return MDI_RES_CAMERA_SUCCEED;
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_to_file
 * DESCRIPTION
 *  Capture one jpeg file
 * PARAMETERS
 *  file_name               [IN]        Filename buffer pointer         
 *  continue_capture        [IN]        Continue capture flag
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_to_file(S8 *file_name, U8 continue_capture)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MMI_BOOL tvout_need_to_sleep_out = MMI_FALSE;
#endif /* __MMI_TVOUT__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }
#if MDI_CAMERA_MT6225_SERIES
    capture_data.memory_output = FALSE;
#endif
    capture_data.image_width = camera_setting_p->image_width;
    capture_data.image_height = camera_setting_p->image_height;

    capture_data.capture_buffer_p = NULL;
    capture_data.file_size_p = NULL;

    capture_data.media_mode = MED_MODE_FILE;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_ISP;   /* ISP */
    capture_data.data = (void*)file_name;
    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.snapshot_number = 1;
    capture_data.gray_mode = 0;
    capture_data.continue_capture = continue_capture;

#if (MDI_CAMERA_MT6238_SERIES)
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_NORMAL_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_pitch_mode = FALSE;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */

    mmi_camera_fill_overlay_data(&capture_data);
    
    capture_data.display = FALSE;
    capture_data.tv_output = FALSE;

    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.lcm_start_x = g_mdi_camera_cntx.lcm_start_x;
    capture_data.lcm_start_y = g_mdi_camera_cntx.lcm_start_y;
    capture_data.lcm_end_x = g_mdi_camera_cntx.lcm_end_x;
    capture_data.lcm_end_y = g_mdi_camera_cntx.lcm_end_y;
    capture_data.update_layer = g_mdi_camera_cntx.update_layer;
    capture_data.hw_update_layer = g_mdi_camera_cntx.hw_update_layer;
    capture_data.frame_buffer_address = (kal_uint32) g_mdi_camera_cntx.frame_buf_ptr;
    capture_data.frame_buffer_size = g_mdi_camera_cntx.frame_buf_size;
    capture_data.roi_offset_x = g_mdi_camera_cntx.roi_offset_x;
    capture_data.roi_offset_y = g_mdi_camera_cntx.roi_offset_y;

#elif (MDI_CAMERA_MT6228_SERIES)
    capture_data.thumbnail_mode = FALSE;
    #ifdef __MTK_TARGET__
    capture_data.image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif
    capture_data.image_pitch_mode = FALSE;
    capture_data.tv_output = FALSE;
    capture_data.manual_wb = FALSE;
    capture_data.display = FALSE;
    capture_data.overlay_frame_mode = camera_setting_p->overlay_frame_mode;
    capture_data.overlay_color_depth = camera_setting_p->overlay_frame_depth;
    capture_data.overlay_frame_source_key = camera_setting_p->overlay_frame_source_key;
    capture_data.overlay_frame_width = camera_setting_p->overlay_frame_width;
    capture_data.overlay_frame_height = camera_setting_p->overlay_frame_height;
    capture_data.overlay_frame_buffer_address = camera_setting_p->overlay_frame_buffer_address;
#endif /* MDI_CAMERA_MT6228_SERIES */

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
    {
        mdi_tvout_sleep_in();
        tvout_need_to_sleep_out = MMI_TRUE;
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && tvout_need_to_sleep_out == MMI_TRUE)
    {
        mdi_tvout_sleep_out();
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_layer_to_file
 * DESCRIPTION
 *  Capture from one of layers to a jpeg file
 * PARAMETERS
 *  capture_layer_flag      [IN]        Layer configuration
 *  file_name               [IN]        File_name buffer pointer
 *  offset_x                [IN]        Capture region's offset x
 *  offset_y                [IN]        Capture region's offset y
 *  width                   [IN]        Capture region's width
 *  height                  [IN]        Capture region's height
 *  continue_capture        [IN]        Continue capture flag
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_layer_to_file(
            U32 capture_layer_flag,
            S8 *file_name,
            S32 offset_x,
            S32 offset_y,
            S32 width,
            S32 height,
            U8 continue_capture)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MMI_BOOL tvout_need_to_sleep_out = MMI_FALSE;
#endif /* __MMI_TVOUT__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }

    capture_data.image_width = width;
    capture_data.image_height = height;

    capture_data.media_mode = MED_MODE_FILE;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_MEM;   /* ISP */
    capture_data.data = (void*)file_name;
    capture_data.capture_layer = capture_layer_flag;
    capture_data.roi_offset_x = GDI_LAYER_OFFSET + offset_x;
    capture_data.roi_offset_y = GDI_LAYER_OFFSET + offset_y;
    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.snapshot_number = 1;
    capture_data.gray_mode = 0;
    capture_data.continue_capture = continue_capture;

#if (MDI_CAMERA_MT6238_SERIES)
    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_NORMAL_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_pitch_mode = FALSE;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */

    capture_data.display = FALSE;    /* diplay to LCM */
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.lcm_start_x = g_mdi_camera_cntx.lcm_start_x;
    capture_data.lcm_start_y = g_mdi_camera_cntx.lcm_start_y;
    capture_data.lcm_end_x = g_mdi_camera_cntx.lcm_end_x;
    capture_data.lcm_end_y = g_mdi_camera_cntx.lcm_end_y;
    capture_data.update_layer = g_mdi_camera_cntx.update_layer;
    capture_data.hw_update_layer = g_mdi_camera_cntx.hw_update_layer;
    capture_data.frame_buffer_address = (kal_uint32) g_mdi_camera_cntx.frame_buf_ptr;
    capture_data.frame_buffer_size = g_mdi_camera_cntx.frame_buf_size;
    capture_data.roi_offset_x = g_mdi_camera_cntx.roi_offset_x;
    capture_data.roi_offset_y = g_mdi_camera_cntx.roi_offset_y;

    mmi_camera_fill_overlay_data(&capture_data);

    capture_data.tv_output = FALSE;    
#elif (MDI_CAMERA_MT6228_SERIES)
    capture_data.manual_wb = FALSE;
    capture_data.thumbnail_mode = FALSE;
    capture_data.overlay_frame_mode = FALSE;
    #ifdef __MTK_TARGET__
    capture_data.image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif
    capture_data.image_pitch_mode = FALSE;
    capture_data.tv_output = FALSE;

    capture_data.update_layer = capture_layer_flag;
    capture_data.hw_update_layer = camera_preview_p->preview_layer_flag;

    capture_data.overlay_frame_mode = camera_setting_p->overlay_frame_mode;
    capture_data.overlay_color_depth = camera_setting_p->overlay_frame_depth;
    capture_data.overlay_frame_source_key = camera_setting_p->overlay_frame_source_key;
    capture_data.overlay_frame_width = camera_setting_p->overlay_frame_width;
    capture_data.overlay_frame_height = camera_setting_p->overlay_frame_height;
    capture_data.overlay_frame_buffer_address = camera_setting_p->overlay_frame_buffer_address;
#endif /* MDI_CAMERA_MT6228_SERIES */

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
    {
        mdi_tvout_sleep_in();
        tvout_need_to_sleep_out = MMI_TRUE;
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif


#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && tvout_need_to_sleep_out == MMI_TRUE)
    {
        mdi_tvout_sleep_out();
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_to_memory_direct_couple
 * DESCRIPTION
 *  Capture jpeg to a memory
 * PARAMETERS
 *  buf_pp              [OUT]       Pointer to a buffer pointer for jpeg file
 *  captured_size       [OUT]       Captured size
 *  dc_buf_addr         [IN]        Captured image layer address
 *  buf_size            [IN]        Captured image layer buffer length
 *  width               [IN]        Width of captured image image
 *  height              [OUT]       Height of captured image image
 *  filename            [IN]        File name
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_to_memory_direct_couple(U8 **buf_pp, U32 *captured_size, U32 dc_buf_addr, S32 buf_size, S32 width, S32 height, S8* filename)
{
#if MDI_CAMERA_MT6225_SERIES
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MMI_BOOL tvout_need_to_sleep_out = MMI_FALSE;
#endif /* __MMI_TVOUT__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }

    capture_data.memory_output = TRUE;
    capture_data.memory_output_width = width;
    capture_data.memory_output_height = height;
    capture_data.memory_output_buffer_address = dc_buf_addr;
    capture_data.memory_output_buffer_size = buf_size; 
    capture_data.image_width = camera_setting_p->image_width;
    capture_data.image_height = camera_setting_p->image_height;

    capture_data.media_mode = MED_MODE_BUFFER;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_ISP;   /* ISP */
    capture_data.capture_buffer_p = (void **)buf_pp;
    capture_data.data = (void*) filename;
    capture_data.file_buffer_len = 0;           /* not used */
    capture_data.file_size_p = (kal_uint32*) captured_size;
    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.snapshot_number = 1;
    capture_data.gray_mode = 0;

#if (MDI_CAMERA_MT6238_SERIES)
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_NORMAL_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_pitch_mode = FALSE;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */

    mmi_camera_fill_overlay_data(&capture_data);

    capture_data.display = FALSE;
    capture_data.tv_output = FALSE;
#elif (MDI_CAMERA_MT6228_SERIES)
    capture_data.thumbnail_mode = FALSE;
    #ifdef __MTK_TARGET__
    capture_data.image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif
    capture_data.image_pitch_mode = FALSE;
    capture_data.tv_output = FALSE;
    capture_data.manual_wb = FALSE;
    capture_data.display = FALSE;

    capture_data.overlay_frame_mode = camera_setting_p->overlay_frame_mode;
    capture_data.overlay_color_depth = camera_setting_p->overlay_frame_depth;
    capture_data.overlay_frame_source_key = camera_setting_p->overlay_frame_source_key;
    capture_data.overlay_frame_width = camera_setting_p->overlay_frame_width;
    capture_data.overlay_frame_height = camera_setting_p->overlay_frame_height;
    capture_data.overlay_frame_buffer_address = camera_setting_p->overlay_frame_buffer_address;
#endif /* MDI_CAMERA_MT6228_SERIES */

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
    {
        mdi_tvout_sleep_in();
        tvout_need_to_sleep_out = MMI_TRUE;
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && tvout_need_to_sleep_out == MMI_TRUE)
    {
        mdi_tvout_sleep_out();
    }
#endif /* __MMI_TVOUT__ */ 

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif
    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }

#else     
    return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
#endif

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_to_memory_continuous_shot_direct_couple
 * DESCRIPTION
 *  Capture jpeg to a memory
 * PARAMETERS
 *  buf_pp              [OUT]       Pointer to a buffer pointer for jpeg file
 *  captured_size       [OUT]       Captured size
 *  dc_buf_addr         [IN]        Captured image layer address
 *  buf_size            [IN]        Captured image layer buffer length
 *  width               [IN]        Width of captured image image
 *  height              [OUT]       Height of captured image image
 *  filename            [IN]        File name
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_to_memory_continuous_shot_direct_couple(U8 **buf_pp, U32 *captured_size, U32 dc_buf_addr, S32 buf_size, S32 width, S32 height, S8* filename)
{
#if MDI_CAMERA_MT6225_SERIES
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MMI_BOOL tvout_need_to_sleep_out = MMI_FALSE;
#endif /* __MMI_TVOUT__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    capture_data.memory_output = TRUE;
    capture_data.memory_output_width = width;
    capture_data.memory_output_height = height;
    capture_data.memory_output_buffer_address = dc_buf_addr;
    capture_data.memory_output_buffer_size = buf_size; 
    capture_data.image_width = camera_setting_p->image_width;
    capture_data.image_height = camera_setting_p->image_height;

    capture_data.media_mode = MED_MODE_BUFFER;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_ISP;   /* ISP */
    capture_data.capture_buffer_p = (void **)buf_pp;
    capture_data.data = (void*) filename;
    capture_data.file_buffer_len = 0;           /* not used */
    capture_data.file_size_p = (kal_uint32*) captured_size;
    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.snapshot_number = 1;
    capture_data.gray_mode = 0;
    capture_data.continue_capture = TRUE;

#if (MDI_CAMERA_MT6238_SERIES)
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_NORMAL_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_pitch_mode = FALSE;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */

    mmi_camera_fill_overlay_data(&capture_data);

    capture_data.display = FALSE;
    capture_data.tv_output = FALSE;
#elif (MDI_CAMERA_MT6228_SERIES)
    capture_data.thumbnail_mode = FALSE;
    #ifdef __MTK_TARGET__
    capture_data.image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif
    capture_data.image_pitch_mode = FALSE;
    capture_data.tv_output = FALSE;
    capture_data.manual_wb = FALSE;
    capture_data.display = FALSE;

    capture_data.overlay_frame_mode = camera_setting_p->overlay_frame_mode;
    capture_data.overlay_color_depth = camera_setting_p->overlay_frame_depth;
    capture_data.overlay_frame_source_key = camera_setting_p->overlay_frame_source_key;
    capture_data.overlay_frame_width = camera_setting_p->overlay_frame_width;
    capture_data.overlay_frame_height = camera_setting_p->overlay_frame_height;
    capture_data.overlay_frame_buffer_address = camera_setting_p->overlay_frame_buffer_address;
#endif /* MDI_CAMERA_MT6228_SERIES */

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
    {
        mdi_tvout_sleep_in();
        tvout_need_to_sleep_out = MMI_TRUE;
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && tvout_need_to_sleep_out == MMI_TRUE)
    {
        mdi_tvout_sleep_out();
    }
#endif /* __MMI_TVOUT__ */ 

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif
    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }

#else     
    return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
#endif /* MDI_CAMERA_MT6225_SERIES */

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_layer_to_memory_direct_couple
 * DESCRIPTION
 *  Capture from layers to a memory
 * PARAMETERS
 *  capture_layer_flag      [IN]        Layer configuration
 *  buf_pp                  [OUT]       Pointer to a buffer pointer for jpeg file
 *  captured_size           [OUT]       Captured size
 *  offset_x                [IN]        Capture region's offset x
 *  offset_y                [IN]        Capture region's offset y
 *  width                   [IN]        Capture region's width
 *  height                  [IN]        Capture region's height
 *  dc_buf_addr             [IN]        Captured image layer address
 *  buf_size                [IN]        Captured image layer buffer length
 *  dc_width                [IN]        Width of display image image
 *  dc_height               [IN]        Height of display image image
 *  filename                [IN]        File name
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_layer_to_memory_direct_couple(
                    U32 capture_layer_flag,
                    U8 **buf_pp,
                    U32 *captured_size,
                    S32 offset_x,
                    S32 offset_y,
                    S32 width,
                    S32 height,
                    U32 dc_buf_addr, S32 buf_size, S32 dc_width, S32 dc_height, S8* filename)
{
#if (MDI_CAMERA_MT6225_SERIES) || defined(__DIRECT_SENSOR_SUPPORT__)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MMI_BOOL tvout_need_to_sleep_out = MMI_FALSE;
#endif /* __MMI_TVOUT__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }

    capture_data.memory_output = TRUE;
    capture_data.memory_output_width = dc_width;
    capture_data.memory_output_height = dc_height;
    capture_data.memory_output_buffer_address = dc_buf_addr;
    capture_data.memory_output_buffer_size = buf_size; 

    capture_data.image_width = width;
    capture_data.image_height = height;

    capture_data.media_mode = MED_MODE_BUFFER;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_MEM;   /* ISP */
    capture_data.capture_buffer_p = (void **)buf_pp;
    capture_data.data = (void*)filename;
    capture_data.file_buffer_len = 0;           /* not used */
    capture_data.file_size_p = (kal_uint32*) captured_size;
    capture_data.capture_layer = capture_layer_flag;
    capture_data.roi_offset_x = GDI_LAYER_OFFSET + offset_x + camera_preview_p->preview_wnd_offset_x;
    capture_data.roi_offset_y = GDI_LAYER_OFFSET + offset_y + camera_preview_p->preview_wnd_offset_y;

    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.snapshot_number = 1;
    capture_data.gray_mode = 0;

#if (MDI_CAMERA_MT6228_SERIES)
    capture_data.manual_wb = camera_setting_p->manual_wb;
    capture_data.thumbnail_mode = FALSE;
    #ifdef __MTK_TARGET__
    capture_data.image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif
    capture_data.image_pitch_mode = FALSE;
    capture_data.tv_output = FALSE;
    capture_data.overlay_frame_mode = FALSE;

    capture_data.update_layer = capture_layer_flag;
    capture_data.hw_update_layer = camera_preview_p->preview_layer_flag;
#endif /* MDI_CAMERA_MT6228_SERIES */

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
    {
        mdi_tvout_sleep_in();
        tvout_need_to_sleep_out = MMI_TRUE;
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && tvout_need_to_sleep_out == MMI_TRUE)
    {
        mdi_tvout_sleep_out();
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else if (result == MED_RES_NOT_ENOUGH_SPACE)
    {
        return MDI_RES_CAMERA_ERR_MEMORY_NOT_ENOUGH;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }
#else     
    return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
#endif /* #if (MDI_CAMERA_MT6225_SERIES) || defined(__DIRECT_SENSOR_SUPPORT__) */

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_to_memory
 * DESCRIPTION
 *  Capture a jpeg image and store the image in memory
 * PARAMETERS
 *  buf_pp              [OUT]       Pointer to a buffer pointer for jpeg file
 *  captured_size       [OUT]       Captured size
 *  file_name           [IN]        Filename buffer pointer
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_to_memory(U8 **buf_pp, U32 *captured_size,S8 *file_name)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MMI_BOOL tvout_need_to_sleep_out = MMI_FALSE;
#endif /* __MMI_TVOUT__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }

    capture_data.image_width = camera_setting_p->image_width;
    capture_data.image_height = camera_setting_p->image_height;

    capture_data.media_mode = MED_MODE_BUFFER;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_ISP;   /* ISP */
    capture_data.capture_buffer_p = (void **)buf_pp;
    capture_data.data = (void*)file_name;
    capture_data.file_buffer_len = 0;           /* not used */
    capture_data.file_size_p = (kal_uint32*) captured_size;
    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.snapshot_number = 1;
    capture_data.gray_mode = 0;
    capture_data.continue_capture = FALSE;

#if (MDI_CAMERA_MT6238_SERIES)
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_NORMAL_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_pitch_mode = FALSE;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */

    mmi_camera_fill_overlay_data(&capture_data);

    capture_data.display = FALSE;
    capture_data.tv_output = FALSE;

    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.lcm_start_x = g_mdi_camera_cntx.lcm_start_x;
    capture_data.lcm_start_y = g_mdi_camera_cntx.lcm_start_y;
    capture_data.lcm_end_x = g_mdi_camera_cntx.lcm_end_x;
    capture_data.lcm_end_y = g_mdi_camera_cntx.lcm_end_y;
    capture_data.update_layer = g_mdi_camera_cntx.update_layer;
    capture_data.hw_update_layer = g_mdi_camera_cntx.hw_update_layer;
    capture_data.frame_buffer_address = (kal_uint32) g_mdi_camera_cntx.frame_buf_ptr;
    capture_data.frame_buffer_size = g_mdi_camera_cntx.frame_buf_size;
    capture_data.roi_offset_x = g_mdi_camera_cntx.roi_offset_x;
    capture_data.roi_offset_y = g_mdi_camera_cntx.roi_offset_y;
    
#elif (MDI_CAMERA_MT6228_SERIES)
    capture_data.thumbnail_mode = FALSE;
    #ifdef __MTK_TARGET__
    capture_data.image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif
    capture_data.image_pitch_mode = FALSE;
    capture_data.tv_output = FALSE;
    capture_data.manual_wb = FALSE;
    capture_data.display = TRUE;    /* diplay to LCM */
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.lcm_start_x = g_mdi_camera_cntx.lcm_start_x;
    capture_data.lcm_start_y = g_mdi_camera_cntx.lcm_start_y;
    capture_data.lcm_end_x = g_mdi_camera_cntx.lcm_end_x;
    capture_data.lcm_end_y = g_mdi_camera_cntx.lcm_end_y;
    capture_data.update_layer = g_mdi_camera_cntx.update_layer;
    capture_data.hw_update_layer = g_mdi_camera_cntx.hw_update_layer;
    capture_data.frame_buffer_address = (kal_uint32) g_mdi_camera_cntx.frame_buf_ptr;
    capture_data.frame_buffer_size = g_mdi_camera_cntx.frame_buf_size;
    capture_data.roi_offset_x = g_mdi_camera_cntx.roi_offset_x;
    capture_data.roi_offset_y = g_mdi_camera_cntx.roi_offset_y;

    capture_data.overlay_frame_mode = camera_setting_p->overlay_frame_mode;
    capture_data.overlay_color_depth = camera_setting_p->overlay_frame_depth;
    capture_data.overlay_frame_source_key = camera_setting_p->overlay_frame_source_key;
    capture_data.overlay_frame_width = camera_setting_p->overlay_frame_width;
    capture_data.overlay_frame_height = camera_setting_p->overlay_frame_height;
    capture_data.overlay_frame_buffer_address = camera_setting_p->overlay_frame_buffer_address;

    capture_data.tv_output = FALSE;
#endif /* MDI_CAMERA_MT6228_SERIES */

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
    {
        mdi_tvout_sleep_in();
        tvout_need_to_sleep_out = MMI_TRUE;
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && tvout_need_to_sleep_out == MMI_TRUE)
    {
        mdi_tvout_sleep_out();
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_adjust_mwb
 * DESCRIPTION
 *  Capture from layers to a memory, then cal mwb
 * PARAMETERS
 *  capture_layer_flag      [IN]        Layer configuration
 *  offset_x                [IN]        Capture region's offset x
 *  offset_y                [IN]        Capture region's offset y
 *  width                   [IN]        Capture region's width
 *  height                  [IN]        Capture region's height
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_adjust_mwb(U32 capture_layer_flag, S32 offset_x, S32 offset_y, S32 width, S32 height)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MDI_RESULT ret;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->manual_wb = TRUE;
    ret = mdi_camera_capture_layer_to_memory(capture_layer_flag, NULL, NULL, offset_x, offset_y, width, height,NULL);
    camera_setting_p->manual_wb = FALSE;

    return ret;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_layer_to_memory
 * DESCRIPTION
 *  capture from layers to a memory, this is used for add frame
 * PARAMETERS
 *  capture_layer_flag      [IN]        Layer configuration
 *  buf_pp                  [OUT]       Pointer to a buffer pointer for jpeg file
 *  offset_x                [IN]        Capture region's offset x
 *  offset_y                [IN]        Capture region's offset y
 *  width                   [IN]        Capture region's width
 *  height                  [IN]        Capture region's height
 *  file_name               [IN]        File_name buffer pointer
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_layer_to_memory(
            U32 capture_layer_flag,
            U8 **buf_pp,
            U32 *captured_size,
            S32 offset_x,
            S32 offset_y,
            S32 width,
            S32 height,
            S8 *filename)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MMI_BOOL tvout_need_to_sleep_out = MMI_FALSE;
#endif /* __MMI_TVOUT__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }

    capture_data.image_width = width;
    capture_data.image_height = height;

    capture_data.media_mode = MED_MODE_BUFFER;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_MEM;   /* ISP */
    capture_data.capture_buffer_p = (void **)buf_pp;
    capture_data.data = (void*)filename;
    capture_data.file_buffer_len = 0;           /* not used */
    capture_data.file_size_p = (kal_uint32*) captured_size;
    capture_data.capture_layer = capture_layer_flag;
    capture_data.roi_offset_x = GDI_LAYER_OFFSET + offset_x + camera_preview_p->preview_wnd_offset_x;
    capture_data.roi_offset_y = GDI_LAYER_OFFSET + offset_y + camera_preview_p->preview_wnd_offset_y;

    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.snapshot_number = 1;
    capture_data.gray_mode = 0;

#if (MDI_CAMERA_MT6238_SERIES)
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_NORMAL_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_pitch_mode = FALSE;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */

    mmi_camera_fill_overlay_data(&capture_data);

    capture_data.display = FALSE;
    capture_data.tv_output = FALSE;
#elif (MDI_CAMERA_MT6228_SERIES)
    capture_data.manual_wb = camera_setting_p->manual_wb;
    capture_data.thumbnail_mode = FALSE;
    #ifdef __MTK_TARGET__
    capture_data.image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif
    capture_data.image_pitch_mode = FALSE;
    capture_data.tv_output = FALSE;
    capture_data.overlay_frame_mode = FALSE;

    capture_data.update_layer = capture_layer_flag;
    capture_data.hw_update_layer = camera_preview_p->preview_layer_flag;

    capture_data.overlay_frame_mode = camera_setting_p->overlay_frame_mode;
    capture_data.overlay_color_depth = camera_setting_p->overlay_frame_depth;
    capture_data.overlay_frame_source_key = camera_setting_p->overlay_frame_source_key;
    capture_data.overlay_frame_width = camera_setting_p->overlay_frame_width;
    capture_data.overlay_frame_height = camera_setting_p->overlay_frame_height;
    capture_data.overlay_frame_buffer_address = camera_setting_p->overlay_frame_buffer_address;
#endif /* MDI_CAMERA_MT6228_SERIES */

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
    {
        mdi_tvout_sleep_in();
        tvout_need_to_sleep_out = MMI_TRUE;
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && tvout_need_to_sleep_out == MMI_TRUE)
    {
        mdi_tvout_sleep_out();
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else if (result == MED_RES_NOT_ENOUGH_SPACE)
    {
        return MDI_RES_CAMERA_ERR_MEMORY_NOT_ENOUGH;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_burst_capture_to_memory
 * DESCRIPTION
 *  Burst shot capture to memory
 * PARAMETERS
 *  capture_count       [IN]        Capture number       
 *  jpgs_ptr            [OUT]       Captured jpeg info
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_MEMORY_NOT_ENOUGH    Media camera buffer isn't enough
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_burst_capture_to_memory(U32 capture_count, mdi_camera_jpegs_struct *jpgs_ptr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }

    capture_data.image_width = camera_setting_p->image_width;
    capture_data.image_height = camera_setting_p->image_height;
    capture_data.media_mode = MED_MODE_BUFFER;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_ISP;   /* ISP */

    capture_data.data = NULL;               /* not used */
    capture_data.file_buffer_len = 0;       /* not used */
    capture_data.file_size_p = NULL;        /* not used */
    capture_data.capture_buffer_p = NULL;   /* not used */

    capture_data.snapshot_number = capture_count;
    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.gray_mode = 0;
#ifdef __MTK_TARGET__
    capture_data.jpegs_p = (media_jpegs_struct*) jpgs_ptr;
#endif 

#if (MDI_CAMERA_MT6238_SERIES)
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_NORMAL_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_pitch_mode = FALSE;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */

    mmi_camera_fill_overlay_data(&capture_data);

    capture_data.display = FALSE;
    capture_data.tv_output = FALSE;
#elif (MDI_CAMERA_MT6228_SERIES)
    capture_data.thumbnail_mode = FALSE;
    capture_data.manual_wb = FALSE;
    #ifdef __MTK_TARGET__
    capture_data.image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif
    capture_data.image_pitch_mode = FALSE;
    capture_data.tv_output = FALSE;

    capture_data.overlay_frame_mode = camera_setting_p->overlay_frame_mode;
    capture_data.overlay_color_depth = camera_setting_p->overlay_frame_depth;
    capture_data.overlay_frame_source_key = camera_setting_p->overlay_frame_source_key;
    capture_data.overlay_frame_width = camera_setting_p->overlay_frame_width;
    capture_data.overlay_frame_height = camera_setting_p->overlay_frame_height;
    capture_data.overlay_frame_buffer_address = camera_setting_p->overlay_frame_buffer_address;
#endif /* MDI_CAMERA_MT6228_SERIES */

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_NOT_ENOUGH_SPACE)
    {
        return MDI_RES_CAMERA_ERR_MEMORY_NOT_ENOUGH;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_non_block_burst_capture_to_memory
 * DESCRIPTION
 *  Non-block burst shot capture to memory
 * PARAMETERS
 *  capture_count               [IN]        Capture number
 *  jpgs_ptr                    [OUT]       Captured jpeg info        
 *  capture_result_callback     [IN]        Capture result callback
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_MEMORY_NOT_ENOUGH    Media camera buffer isn't enough
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_non_block_burst_capture_to_memory(
            U32 capture_count,
            mdi_camera_jpegs_struct *jpgs_ptr,
            void (*capture_result_callback) (MDI_RESULT, U16))
{
#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MDI_RESULT ret;
#endif
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }

    ASSERT(capture_result_callback != NULL);

    g_mdi_camera_cntx.burst_cap_count = capture_count;

    mdi_camera_non_block_burst_shot_result_callback = capture_result_callback;
    SetProtocolEventHandler(mdi_camera_non_block_burst_capture_result_hdlr, MSG_ID_MEDIA_CAM_CAPTURE_EVENT_IND);

    capture_data.image_width = camera_setting_p->image_width;
    capture_data.image_height = camera_setting_p->image_height;
    capture_data.media_mode = MED_MODE_BUFFER;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_ISP;   /* ISP */

    capture_data.data = NULL;               /* not used */
    capture_data.file_buffer_len = 0;       /* not used */
    capture_data.file_size_p = NULL;        /* not used */
    capture_data.capture_buffer_p = NULL;   /* not used */

    capture_data.snapshot_number = capture_count;
    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.gray_mode = 0;
    capture_data.continue_capture = TRUE;
#ifdef __MTK_TARGET__
    capture_data.jpegs_p = (media_jpegs_struct*) jpgs_ptr;
#endif 

#if (MDI_CAMERA_MT6238_SERIES)
    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_NORMAL_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */
#elif (MDI_CAMERA_MT6228_SERIES)
    capture_data.manual_wb = FALSE;
    capture_data.thumbnail_mode = FALSE;
    #ifdef __MTK_TARGET__
    capture_data.image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif
#endif

    capture_data.image_pitch_mode = FALSE;

    capture_data.display = TRUE;    /* diplay to LCM */
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.lcm_start_x = g_mdi_camera_cntx.lcm_start_x;
    capture_data.lcm_start_y = g_mdi_camera_cntx.lcm_start_y;
    capture_data.lcm_end_x = g_mdi_camera_cntx.lcm_end_x;
    capture_data.lcm_end_y = g_mdi_camera_cntx.lcm_end_y;
    capture_data.update_layer = g_mdi_camera_cntx.update_layer;
    capture_data.hw_update_layer = g_mdi_camera_cntx.hw_update_layer;
    capture_data.frame_buffer_address = (kal_uint32) g_mdi_camera_cntx.frame_buf_ptr;
    capture_data.frame_buffer_size = g_mdi_camera_cntx.frame_buf_size;
    capture_data.roi_offset_x = g_mdi_camera_cntx.roi_offset_x;
    capture_data.roi_offset_y = g_mdi_camera_cntx.roi_offset_y;

    mmi_camera_fill_overlay_data(&capture_data);

    capture_data.tv_output = FALSE;

    /* TV out */
#ifdef __MMI_TVOUT__

    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            /* set tvout owner to Camera */
            ret = mdi_tvout_get_para(
                    MDI_TV_OWNER_CAMERA,
                    NULL,                                   /* mode_ptr */
                    (PU16) & capture_data.tv_output_width,  /* buf_width_ptr */
                    (PU16) & capture_data.tv_output_height, /* buf_height_ptr */
                    (PU32) & capture_data.tv_output_buffer_size,       /* buf size */
                    (PU8*) & capture_data.tv_output_buffer1_address,  /* buf1 pp */
                    (PU8*) & capture_data.tv_output_buffer2_address,  /* buf2 pp */
                    NULL,                                   /* rotate_buf_1_pp */
                    NULL);                                  /* rotate_buf_2_pp */

            if (ret == MDI_RES_TV_ENABLE)
            {
                capture_data.tv_output = TRUE;
                capture_data.tv_output_offset_x = g_mdi_camera_cntx.tv_output_offset_x;
                capture_data.tv_output_offset_y = g_mdi_camera_cntx.tv_output_offset_y;
            }

        }   /* tvout is enable or not */
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_NOT_ENOUGH_SPACE)
    {
        return MDI_RES_CAMERA_ERR_MEMORY_NOT_ENOUGH;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }
#else /* MDI_CAMERA_MT6228_SERIES */ 
    /* only availabe on MT6228 */
    return MDI_RES_CAMERA_ERR_FAILED;
#endif /* MDI_CAMERA_MT6228_SERIES */ 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_stop_non_block_burst_capture
 * DESCRIPTION
 *  Stop non blocking burst capture
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED  Success
 *****************************************************************************/
MDI_RESULT mdi_camera_stop_non_block_burst_capture(void)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES)
    media_cam_stop(stack_int_get_active_module_id());

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

#endif /* MDI_CAMERA_MT6228_SERIES */ 
#endif /* __MTK_TARGET__ */ 

    return MDI_RES_CAMERA_SUCCEED;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_non_block_burst_capture_result_hdlr
 * DESCRIPTION
 *  burst capture result handler
 * PARAMETERS
 *  msg_ptr     [?]     
 * RETURNS
 *  void
 * BOOL
 *****************************************************************************/
#if (MDI_CAMERA_MT6228_SERIES) || (MDI_CAMERA_MT6238_SERIES)
static void mdi_camera_non_block_burst_capture_result_hdlr(void *msg_ptr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_event_ind_struct *ind_p;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ind_p = (media_cam_capture_event_ind_struct*) msg_ptr;

    /* note:capture count start from 1 */

    if ((ind_p->result != MED_RES_OK) ||
        ((ind_p->result == MED_RES_OK) && ((ind_p->count) == g_mdi_camera_cntx.burst_cap_count)))
    {
        /* if failed, or if it is last capture - will enter here */

        g_mdi_camera_cntx.burst_cap_count = 0;

        /* capture will also stop preview TV-Out */
    #ifdef __MMI_TVOUT__
        /* restore owner to GDI */
        if (g_mdi_camera_cntx.is_tvout)
        {
            if (mdi_tvout_is_enable())
            {
                if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
                {
                    mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
                }
            }
        }
    #endif /* __MMI_TVOUT__ */ 

    }

    /* successful save file */
    if (ind_p->result == MED_RES_OK)
    {
        mdi_camera_non_block_burst_shot_result_callback(MDI_RES_CAMERA_SUCCEED, ind_p->count);
    }
    else if (ind_p->result == MED_RES_BUFFER_INSUFFICIENT)
    {
        mdi_camera_non_block_burst_shot_result_callback(MDI_RES_CAMERA_ERR_MEMORY_NOT_ENOUGH, ind_p->count);
    }
    else
    {
        mdi_camera_non_block_burst_shot_result_callback(MDI_RES_CAMERA_ERR_FAILED, ind_p->count);
    }

}
#endif /* MDI_CAMERA_MT6228_SERIES */ 


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_is_ready_to_capture
 * DESCRIPTION
 *  Check hw module is ready to capture 
 *  (some module requires some time to power on and init)
 * PARAMETERS
 *  void
 * RETURNS
 *  TRUE        Ready
 *  FALSE       Not ready
 *****************************************************************************/
BOOL mdi_camera_is_ready_to_capture(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_banding
 * DESCRIPTION
 *  Update banding value
 * PARAMETERS
 *  cam_para        [IN]        Banding value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_banding(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->banding = cam_para;
    
#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_BANDING, cam_para);
#endif 
    
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_ev
 * DESCRIPTION
 *  Update ev setting value
 * PARAMETERS
 *  cam_para        [IN]        EV setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_ev(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(cam_para < MDI_CAMERA_EV_COUNT);

    camera_setting_p->ev = cam_para;

#ifdef __MTK_TARGET__
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_EXPOSURE, cam_para);
    }
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_ev_adjust
 * DESCRIPTION
 *  Update ev setting value - for factory mode to adjust ev balance
 * PARAMETERS
 *  cam_para        [IN]        EV adjust value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_ev_adjust(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_EV_VALUE, cam_para);
    }
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_zoom
 * DESCRIPTION
 *  Update zoom setting value
 * PARAMETERS
 *  cam_para        [IN]        Zoom setting value (10 measn 1x, 40 means 4x, etc)
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_zoom(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->zoom = cam_para;

#ifdef __MTK_TARGET__
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_ZOOM_FACTOR, cam_para);
    }
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_effect
 * DESCRIPTION
 *  Update effect setting value
 * PARAMETERS
 *  cam_para        [IN]        Effect setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_effect(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(cam_para < MDI_CAMERA_EFFECT_COUNT);

    camera_setting_p->effect = cam_para;

#ifdef __MTK_TARGET__
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_EFFECT, cam_para);
    }
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_wb
 * DESCRIPTION
 *  Update wb setting value
 * PARAMETERS
 *  cam_para        [IN]        WB setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_wb(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(cam_para < MDI_CAMERA_WB_COUNT);

    camera_setting_p->wb = cam_para;

#ifdef __MTK_TARGET__
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_WB, cam_para);
    }
#endif /* __MTK_TARGET__ */ 
}



/*****************************************************************************
 * FUNCTION
 *  mdi_camera_restore_mwb
 * DESCRIPTION
 * Restore original mwb value
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_restore_mwb(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_mwb_operation(stack_int_get_active_module_id(), MDI_CAMERA_MWB_RESTORE);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_backup_mwb
 * DESCRIPTION
 *  Backup mwb value
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_backup_mwb(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_mwb_operation(stack_int_get_active_module_id(), MDI_CAMERA_MWB_BACKUP);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_set_overlay_palette
 * DESCRIPTION
 *  Set overlay palette
 * PARAMETERS
 *  palette_size            [IN]        The size of paltte      
 *  palette_addr_ptr        [IN]        The address of paltte
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_set_overlay_palette(U8 palette_size, U32 *palette_addr_ptr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_set_overlay_palette(stack_int_get_active_module_id(), (kal_uint8) palette_size, (kal_uint32*) palette_addr_ptr);
#endif 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_brightness
 * DESCRIPTION
 *  Update brightness setting value
 * PARAMETERS
 *  cam_para        [IN]        Brightness setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_brightness(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->brightness = cam_para;

#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_BRIGHTNESS, cam_para);
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_saturation
 * DESCRIPTION
 *  Update saturation setting value
 * PARAMETERS
 *  cam_para        [IN]        Saturation setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_saturation(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->saturation = cam_para;

#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_SATURATION, cam_para);
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_contrast
 * DESCRIPTION
 *  Update contrast setting value
 * PARAMETERS
 *  cam_para        [IN]        Contrast setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_contrast(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->contrast = cam_para;

#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_CONTRAST, cam_para);
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_hue
 * DESCRIPTION
 *  Update hue setting value
 * PARAMETERS
 *  cam_para        [IN]        Hue setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_hue(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->hue = cam_para;

#ifdef __MTK_TARGET__
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_HUE, cam_para);
    }
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_night
 * DESCRIPTION
 *  Update night mode setting value
 * PARAMETERS
 *  cam_para        [IN]        Night mode setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_night(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->night = cam_para;

#ifdef __MTK_TARGET__
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_NIGHT_MODE, cam_para);
    }
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_image_qty
 * DESCRIPTION
 *  Update image quality setting value
 * PARAMETERS
 *  cam_para        [IN]        Image qty setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_image_qty(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(cam_para < MDI_CAMERA_JPG_QTY_COUNT);
    camera_setting_p->image_qty = cam_para;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_image_size
 * DESCRIPTION
 *  Update image size setting value
 * PARAMETERS
 *  image_width         [IN]        Captured iamge width
 *  image_height        [IN]        Captured iamge height
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_image_size(U16 image_width, U16 image_height)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->image_width = image_width;
    camera_setting_p->image_height = image_height;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_flash
 * DESCRIPTION
 *  Update flash setting value
 * PARAMETERS
 *  cam_para        [IN]        Flash setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_flash(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->flash = cam_para;

#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_FLASH, cam_para);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_iso
 * DESCRIPTION
 *  Update iso setting value
 * PARAMETERS
 *  cam_para        [IN]        ISO setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_iso(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->iso = cam_para;

#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_ISO, cam_para);
#endif 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_ae_meter
 * DESCRIPTION
 *  Update ae meter setting value
 * PARAMETERS
 *  cam_para        [IN]        AE meter setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_ae_meter(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->ae_meter = cam_para;

#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_AE_METERING, cam_para);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_dsc_mode
 * DESCRIPTION
 *  Update scene(DSC) mode value
 * PARAMETERS
 *  cam_para        [IN]        Scene mode setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_dsc_mode(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->dsc_mode = cam_para;

#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_DSC_MODE, cam_para);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_shutter_pri
 * DESCRIPTION
 *  Update shutter priority value
 * PARAMETERS
 *  cam_para        [IN]        Shutter pri
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_shutter_pri(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->shutter_pri = cam_para;

#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_SHUTTER_PRIORITY, cam_para);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_aperture_pri
 * DESCRIPTION
 *  Update apreture priority value
 * PARAMETERS
 *  cam_para        [IN]        Aperture pri
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_aperture_pri(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->aperture_pri = cam_para;

#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_APERTURE_PRIORITY, cam_para);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_af_key
 * DESCRIPTION
 *  Update af key value
 * PARAMETERS
 *  cam_para        [IN]        AF key
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_af_key(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_AF_KEY, cam_para);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_af_mode
 * DESCRIPTION
 *  Update af mode value
 * PARAMETERS
 *  cam_para        [IN]        AF mode
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_af_mode(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->af_mode = cam_para;
#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_AF_MODE, cam_para);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_af_metering
 * DESCRIPTION
 *  Update af metering value
 * PARAMETERS
 *  cam_para        [IN]        AF metering
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_af_metering(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    camera_setting_p->af_metering_mode = cam_para;
#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_AF_METERING, cam_para);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_manual_focus_dir
 * DESCRIPTION
 *  Update manual focus direction value
 * PARAMETERS
 *  cam_para        [IN]        Manual focus dir
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_manual_focus_dir(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_MANUAL_FOCUS_DIR, cam_para);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_ev
 * DESCRIPTION
 *  Return the current ev value
 * PARAMETERS
 *  void
 * RETURNS
 * Camera EV value
 *****************************************************************************/
U16 mdi_camera_get_para_ev(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->ev;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_zoom
 * DESCRIPTION
 *  Return the current zoom step value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam zoom value 
 *****************************************************************************/
U16 mdi_camera_get_para_zoom(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->zoom;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_banding
 * DESCRIPTION
 *  Return the current banding value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam banding value
 *****************************************************************************/
U16 mdi_camera_get_para_banding(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->banding;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_effect
 * DESCRIPTION
 *  Return the current effect value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam effect value
 *****************************************************************************/
U16 mdi_camera_get_para_effect(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->effect;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_wb
 * DESCRIPTION
 *  Return the current wb value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam WB value
 *****************************************************************************/
U16 mdi_camera_get_para_wb(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->wb;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_brightness
 * DESCRIPTION
 *  Return the current brightness value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam brightness value
 *****************************************************************************/
U16 mdi_camera_get_para_brightness(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->brightness;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_saturation
 * DESCRIPTION
 *  Return the current saturation value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam saturation value
 *****************************************************************************/
U16 mdi_camera_get_para_saturation(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->saturation;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_contrast
 * DESCRIPTION
 *  Return the current contrast value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam contrast value
 *****************************************************************************/
U16 mdi_camera_get_para_contrast(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->contrast;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_hue
 * DESCRIPTION
 *  Return the current hue value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam hue value
 *****************************************************************************/
U16 mdi_camera_get_para_hue(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->hue;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_night
 * DESCRIPTION
 *  Return the current night mode value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam night mode value
 *****************************************************************************/
U16 mdi_camera_get_para_night(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->night;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_image_qty
 * DESCRIPTION
 *  Return the current image quality value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam image quality value
 *****************************************************************************/
U16 mdi_camera_get_para_image_qty(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->image_qty;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_image_size
 * DESCRIPTION
 *  Return current image size value
 * PARAMETERS
 *  image width         [OUT]       Current image width
 *  image height        [OUT]       Current image height
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_get_para_image_size(U16* image_width_ptr, U16* image_height_ptr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    *image_width_ptr = camera_setting_p->image_width;
    *image_height_ptr = camera_setting_p->image_height;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_flash
 * DESCRIPTION
 *  Return the current flash value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam flash value
 *****************************************************************************/
U16 mdi_camera_get_para_flash(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->flash;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_iso
 * DESCRIPTION
 *  Return the current iso value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam ISO value
 *****************************************************************************/
U16 mdi_camera_get_para_iso(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->iso;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_ae_meter
 * DESCRIPTION
 *  Return the current ae meter value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam AE meter value
 *****************************************************************************/
U16 mdi_camera_get_para_ae_meter(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->ae_meter;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_dsc_mode
 * DESCRIPTION
 *  Return the current dsc mode value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam dsc mode value
 *****************************************************************************/
U16 mdi_camera_get_para_dsc_mode(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->dsc_mode;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_para_af_mode
 * DESCRIPTION
 *  Return the current af mode value
 * PARAMETERS
 *  void
 * RETURNS
 *  Cam AF mode value
 *****************************************************************************/
U16 mdi_camera_get_para_af_mode(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return camera_setting_p->af_mode;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_is_camera_avaliable
 * DESCRIPTION
 *  Check if camera is avaliable
 * PARAMETERS
 *  void
 * RETURNS
 *  TRUE        Avaliable
 *  FALSE       Not avaliable
 *****************************************************************************/
BOOL mdi_camera_is_camera_avaliable(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MDI_RESULT ret;
    BOOL is_avaliable = FALSE;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ret = mdi_camera_power_on();
    if (ret == MDI_RES_CAMERA_SUCCEED)
    {
        mdi_camera_power_off();
        is_avaliable = TRUE;
    }

    return is_avaliable;

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_stop_autofocus_process
 * DESCRIPTION
 *  Stop auto focus process
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_stop_autofocus_process()
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_AF_KEY, CAM_AF_RELEASE);
#endif 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_af_finish_ind
 * DESCRIPTION
 *  
 * PARAMETERS
 *  inMsg       [?]     
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_af_finish_ind(void *inMsg)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_af_ind_struct *msg_p = (media_cam_af_ind_struct*) inMsg;


    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (g_mdi_camera_af_callback)
    {
        #if (MDI_CAMERA_MT6238_SERIES)
        mdi_camera_af_result_struct af_result;
        af_result.af_result = (mdi_af_result_enum) msg_p->af_search_result;
        af_result.af_success_zone = msg_p->af_success_zone;
        g_mdi_camera_af_callback(af_result);       
        #else
        g_mdi_camera_af_callback((mdi_camera_af_result_struct) msg_p->af_success_zone);
        #endif
    }
    else
    {
        ASSERT(0);
    }
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_stop_mf_continue
 * DESCRIPTION
 *  Stop manual focus
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_stop_mf_continue(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_MANUAL_FOCUS_DIR, CAM_FOCUS_STOP);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_mf_backward
 * DESCRIPTION
 *  Manual focus backward
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_mf_backward(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_MANUAL_FOCUS_DIR, CAM_FOCUS_BACKWARD);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_mf_forward
 * DESCRIPTION
 *  Manual focus forward
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_mf_forward(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_MANUAL_FOCUS_DIR, CAM_FOCUS_FORWARD);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_start_mf_continue
 * DESCRIPTION
 *  Start manual focus
 * PARAMETERS
 *  is_forward      [IN]     Forward/backward   
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_start_mf_continue(BOOL is_forward)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    if (is_forward)
    {
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_MANUAL_FOCUS_DIR, CAM_FOCUS_FORWARD_CONTINUE);
    }
    else
    {
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_MANUAL_FOCUS_DIR, CAM_FOCUS_BACKWARD_CONTINUE);
    }
#endif /* __MTK_TARGET__ */ 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_focus_steps
 * DESCRIPTION
 *  Get total zoom steps and the current zoom step
 * PARAMETERS
 *  total_steps         [OUT]       Total steps
 *  current_steps       [OUT]       The current step
 * RETURNS
 *  void
 *****************************************************************************/
MMI_BOOL mdi_camera_get_focus_steps(U32 *total_steps, U32 *current_steps)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MMI_BOOL flag = FALSE;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    media_cam_get_focus_steps(stack_int_get_active_module_id(), total_steps, current_steps, &flag);
    return flag;
#else /* __MTK_TARGET__ */ 
    return FALSE;
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_start_autofocus_process
 * DESCRIPTION
 *  Start to do auto focus process
 * PARAMETERS
 *  handler         [IN]        Auto focus finish callback function
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_start_autofocus_process(mdi_camera_af_ind_callback handler)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_AF_KEY, CAM_AF_ONE_TIME);
#endif 
    g_mdi_camera_af_callback = handler;
    SetProtocolEventHandler(mdi_camera_af_finish_ind, MSG_ID_MEDIA_CAM_AF_PROCESS_DONE_IND);
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_encode_layer_to_jpeg
 * DESCRIPTION
 *  Encode a layer and save the encode result as a file
 * PARAMETERS
 *  layer_handle        [IN]        Layer handle
 *  file_name           [IN]        Filename for the jpeg file
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_ROOT_DIR_FULL        Root directory full
 *  MDI_RES_CAMERA_ERR_FAILED               Failed
 *****************************************************************************/
MDI_RESULT mdi_camera_encode_layer_to_jpeg(gdi_handle layer_handle, PS8 file_name)
{
#if defined(JPG_ENCODE) && defined(__MTK_TARGET__)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    img_encode_struct encode_data;
    S32 image_width;
    S32 image_height;
    PU8 image_ptr;
    U32 file_size;
    S32 ret;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    gdi_layer_push_and_set_active(layer_handle);
    gdi_layer_get_dimension(&image_width, &image_height);
    gdi_layer_get_buffer_ptr(&image_ptr);
    gdi_layer_pop_and_restore_active();

    encode_data.image_width = image_width;
    encode_data.image_height = image_height;

    encode_data.image_quality = CAM_JPG_QTY_HIGH;
    encode_data.image_buffer_p = (void*)image_ptr;
    encode_data.image_buffer_size = (image_width * image_height * GDI_MAINLCD_BIT_PER_PIXEL) >> 3;

    encode_data.target_width = image_width;
    encode_data.target_height = image_height;
    encode_data.media_mode = MED_MODE_FILE;
    encode_data.media_type = MED_TYPE_JPG;

    encode_data.data = (void*)file_name;
    encode_data.file_size_p = (kal_uint32*) & file_size;
    encode_data.blocking = (kal_bool) TRUE;         /* blocking */
    encode_data.seq_num = (kal_uint16) 0;           /* not used */
    encode_data.file_buffer_len = (kal_uint32) 0;   /* not used */
    encode_data.expected_encode_size = (kal_uint32) 0;   /* not used */
    encode_data.gray_mode = 0;
    encode_data.background_encode = FALSE;
#if (MDI_CAMERA_MT6228_SERIES)
    encode_data.jpeg_yuv_mode = JPEG_FORMAT_YUV420;
    encode_data.thumbnail_mode = FALSE;
    encode_data.overlay_frame_mode = FALSE;
#endif /* MDI_CAMERA_MT6228_SERIES */ 

    ret = media_img_encode(stack_int_get_active_module_id(), &encode_data);

    if (ret == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (ret == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (ret == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (ret == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else if (ret == MED_RES_ROOT_DIR_FULL)
    {
        return MDI_RES_CAMERA_ERR_ROOT_DIR_FULL;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_FAILED;
    }

#else /* defined(JPG_ENCODE) && defined(__MTK_TARGET__) */ 
    return MDI_RES_CAMERA_SUCCEED;
#endif /* defined(JPG_ENCODE) && defined(__MTK_TARGET__) */ 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_add_post_effect
 * DESCRIPTION
 *  Add effect to one of layers
 * PARAMETERS
 *  layer_handle        [IN]        Layer handle 
 *  effect_id           [IN]        Effect index
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_add_post_effect(gdi_handle layer_handle, U16 effect_id)
{
#if defined(ISP_SUPPORT) && defined(__MTK_TARGET__) && (MDI_CAMERA_MT6225_SERIES == 0) 
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    S32 layer_width;
    S32 layer_height;
    PU8 buf_ptr;
    S32 buf_size;
    PU8 work_buf_ptr_1;
    PU8 work_buf_ptr_2;
    U8 layer_vcf;
    IMAGE_EFFECT_DEC_STRUCT effect_data;

    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    gdi_layer_push_and_set_active(layer_handle);
    gdi_layer_get_dimension(&layer_width, &layer_height);
    gdi_layer_get_buffer_ptr(&buf_ptr);
    gdi_layer_pop_and_restore_active();

    buf_size = (layer_width * layer_height * GDI_MAINLCD_BIT_PER_PIXEL) >> 3;

    /* allocate from media task */
    media_get_ext_buffer(
        stack_int_get_active_module_id(),
        (void **)&work_buf_ptr_1,
        ((GDI_LCD_WIDTH * GDI_LCD_HEIGHT * GDI_MAINLCD_BIT_PER_PIXEL) >> 3));

    media_get_ext_buffer(
        stack_int_get_active_module_id(),
        (void **)&work_buf_ptr_2,
        ((GDI_LCD_WIDTH * GDI_LCD_HEIGHT * GDI_MAINLCD_BIT_PER_PIXEL) >> 3));

    /* NOTE: only some effect needs two buffer, like "water paint", etc */

    
    layer_vcf = (U8)((gdi_layer_struct *)layer_handle)->vcf;
    /* TODO: fix when support 888 format */
    switch (layer_vcf)
    {
        case GDI_COLOR_FORMAT_16:
#if defined(DRV_IDP_OLD_DESIGN)
            effect_data.data_type = IBR1_TYPE_RGB565;
#else
            effect_data.src_color_fmt = IMG_COLOR_FMT_RGB565;
            effect_data.dest_color_fmt = IMG_COLOR_FMT_RGB565;
#endif
            break;
        case GDI_COLOR_FORMAT_24:

#if defined(DRV_IDP_OLD_DESIGN)
            effect_data.data_type = IBR1_TYPE_RGB888;
#else
            effect_data.src_color_fmt = IMG_COLOR_FMT_RGB888;
            effect_data.dest_color_fmt = IMG_COLOR_FMT_RGB888;
#endif
            break;
        default:
            ASSERT(0);
    }
#if defined(DRV_IDP_OLD_DESIGN)
    effect_data.data_order = IBR1_ORDER_BGR888; /* not used - just give it a value */
#endif


#if (MDI_CAMERA_MT6228_SERIES)
    effect_data.dest_color_mode = IMGDMA_IBW_OUTPUT_RGB565;
#endif 
    effect_data.source_buffer_address = (kal_uint32) buf_ptr;
    effect_data.dest1_buffer_address = (kal_uint32) work_buf_ptr_1;
    effect_data.dest2_buffer_address = (kal_uint32) work_buf_ptr_2;
    effect_data.image_width = layer_width;
    effect_data.image_height = layer_height;
    effect_data.effect_index = effect_id;
    effect_data.adj_level = 0;                  /* not used */
    effect_data.image_effect_dec_cb = NULL;     /* not used */

    image_effect_dec_process(&effect_data);

    /* copy result to soruce layer */
    memcpy(buf_ptr, work_buf_ptr_1, buf_size);

    /* free buffer */
    media_free_ext_buffer(stack_int_get_active_module_id(), (void **)&work_buf_ptr_1);
    media_free_ext_buffer(stack_int_get_active_module_id(), (void **)&work_buf_ptr_2);

#endif /* defined(ISP_SUPPORT) && defined(__MTK_TARGET__) */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_add_post_adjustment
 * DESCRIPTION
 *  Image adjustment
 * PARAMETERS
 *  layer_handle        [IN]        Layer handle
 *  adjustment_id       [IN]        Adjustment index
 *  value               [IN]        Value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_add_post_adjustment(gdi_handle layer_handle, U16 adjustment_id, S32 value)
{
#if defined(ISP_SUPPORT) && defined(__MTK_TARGET__) && (MDI_CAMERA_MT6225_SERIES == 0)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    S32 layer_width;
    S32 layer_height;
    PU8 buf_ptr;
    S32 buf_size;
    PU8 work_buf_ptr_1;
    PU8 work_buf_ptr_2;
    U8 layer_vcf;
    IMAGE_EFFECT_DEC_STRUCT effect_data;

    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    gdi_layer_push_and_set_active(layer_handle);
    gdi_layer_get_dimension(&layer_width, &layer_height);
    gdi_layer_get_buffer_ptr(&buf_ptr);
    gdi_layer_pop_and_restore_active();

    buf_size = (layer_width * layer_height * GDI_MAINLCD_BIT_PER_PIXEL) >> 3;

    /* allocate from media task */
    media_get_ext_buffer(
        stack_int_get_active_module_id(),
        (void **)&work_buf_ptr_1,
        ((GDI_LCD_WIDTH * GDI_LCD_HEIGHT * GDI_MAINLCD_BIT_PER_PIXEL) >> 3));

    media_get_ext_buffer(
        stack_int_get_active_module_id(),
        (void **)&work_buf_ptr_2,
        ((GDI_LCD_WIDTH * GDI_LCD_HEIGHT * GDI_MAINLCD_BIT_PER_PIXEL) >> 3));

    /* TEMP: HW BUGS on MT6219 - source cant be internal SRAM */
    memcpy(work_buf_ptr_2, buf_ptr, buf_size);
    memset(work_buf_ptr_1, 0, buf_size);

    layer_vcf = (U8)((gdi_layer_struct *)layer_handle)->vcf;
    /* TODO: fix when support 888 format */
    switch (layer_vcf)
    {
        case GDI_COLOR_FORMAT_16:
#if defined(DRV_IDP_OLD_DESIGN)
            effect_data.data_type = IBR1_TYPE_RGB565;
#else
            effect_data.src_color_fmt = IMG_COLOR_FMT_RGB565;
            effect_data.dest_color_fmt = IMG_COLOR_FMT_RGB565;
#endif
            break;
        case GDI_COLOR_FORMAT_24:

#if defined(DRV_IDP_OLD_DESIGN)
            effect_data.data_type = IBR1_TYPE_RGB888;
#else
            effect_data.src_color_fmt = IMG_COLOR_FMT_RGB888;
            effect_data.dest_color_fmt = IMG_COLOR_FMT_RGB888;
#endif
            break;
        default:
            ASSERT(0);
    }
#if defined(DRV_IDP_OLD_DESIGN)
    effect_data.data_order = IBR1_ORDER_BGR888; /* not used - just give it a value */
#endif
#if (MDI_CAMERA_MT6228_SERIES)
    effect_data.dest_color_mode = IMGDMA_IBW_OUTPUT_RGB565;
#endif 
    effect_data.source_buffer_address = (kal_uint32) work_buf_ptr_2;
    effect_data.dest1_buffer_address = (kal_uint32) work_buf_ptr_1;
    effect_data.dest2_buffer_address = (kal_uint32) NULL;   /* not used */
    effect_data.image_width = layer_width;
    effect_data.image_height = layer_height;
    effect_data.effect_index = adjustment_id;
    effect_data.adj_level = value;
    effect_data.image_effect_dec_cb = NULL;                 /* not used */

    image_adj_dec_process(&effect_data);

    /* copy result to soruce layer */
    memcpy(buf_ptr, work_buf_ptr_1, buf_size);

    /* free buffer */
    media_free_ext_buffer(stack_int_get_active_module_id(), (void **)&work_buf_ptr_1);
    media_free_ext_buffer(stack_int_get_active_module_id(), (void **)&work_buf_ptr_2);

#endif /* defined(ISP_SUPPORT) && defined(__MTK_TARGET__) */ 
}
/*******************************************************************************
 * Fast zoomin                                                                 *
 * zoom_limit: MAX ZOOM FACTOR ==> 8x zoom should fill in 80.                  *
 * zoom_step: how many zoom step to be operated per zoomin/zoomout operation.  *
 * zoom_speed: how many frame to be inserted in each zoomin/zoomout zoom step. *
********************************************************************************/
/*****************************************************************************
 * FUNCTION
 *  mdi_camera_start_fast_zoom
 * DESCRIPTION
 *  Start fast zoom [smooth zoom]
 * PARAMETERS
 *  zoom_in         [IN]        Zoom in/out
 *  zoom_limit      [IN]        MAX ZOOM FACTOR ==> 8x zoom should fill in 80.
 *  zoom_step       [IN]        How many zoom step to be operated per zoomin/zoomout operation.
 *  zoom_speed      [IN]        How many frame to be inserted in each zoomin/zoomout zoom step.
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_start_fast_zoom(BOOL zoom_in, U8 zoom_limit, U8 zoom_step, U8 zoom_speed)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    media_cam_start_fast_zoom(stack_int_get_active_module_id(), zoom_in, zoom_limit, zoom_step, zoom_speed);
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_stop_fast_zoom
 * DESCRIPTION
 *  Stop fast zoom
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_stop_fast_zoom(void)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    media_cam_stop_fast_zoom(stack_int_get_active_module_id());
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_fast_zoom_factor
 * DESCRIPTION
 *  Get current zoom factor
 * PARAMETERS
 *  factor      [OUT]   Zoom factor     
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_get_fast_zoom_factor(U32 *factor)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    media_cam_get_fast_zoom_factor(stack_int_get_active_module_id(), factor, NULL);
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_fast_zoom_step
 * DESCRIPTION
 *  Get current zoom factor
 * PARAMETERS
 *  step        [OUT]   Zoom step    
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_get_fast_zoom_step(U32 *step)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    media_cam_get_fast_zoom_factor(stack_int_get_active_module_id(), NULL, step);
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_max_zoom_factor
 * DESCRIPTION
 *  Get max zoom factor
 * PARAMETERS
 *  image_width         [IN]        Image width      
 *  image_height        [IN]        Image height
 * RETURNS
 *  Zoom factor value
 *****************************************************************************/
U16 mdi_camera_get_max_zoom_factor(S32 image_width, S32 image_height)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state == CAMERA_STATE_POWER_OFF)
    {
        return 10;
    }
    else
    {
        return (U16) media_cam_get_max_zoom_factor((U16) image_width, (U16) image_height);
    }
#else /* __MTK_TARGET__ */ 
    return 10;  /* 1x zooming */
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_load_default_setting
 * DESCRIPTION
 *  Get camera default setting
 * PARAMETERS        
 *  camera_setting_data     [OUT]       Camera setting structure
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_load_default_setting(mdi_camera_setting_struct *camera_setting_data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* preview parametes */
    camera_setting_data->wb = MDI_CAMERA_WB_AUTO;
    camera_setting_data->manual_wb = FALSE;
    camera_setting_data->ev = MDI_CAMERA_EV_0;
    camera_setting_data->banding = MDI_CAMERA_BANDING_50HZ;
    camera_setting_data->flash = MDI_CAMERA_FLASH_AUTO;
    camera_setting_data->night = 0;

    camera_setting_data->iso = MDI_CAMERA_ISO_AUTO;
    camera_setting_data->ae_meter = MDI_CAMERA_AE_METER_AUTO;
    camera_setting_data->dsc_mode = MDI_CAMERA_DSC_MODE_AUTO;

#if defined(__MDI_CAMERA_FD_SUPPORT__)
    camera_setting_data->fd_enable = FALSE;
#endif

#if MDI_CAMERA_MT6238_SERIES
    camera_setting_data->zoom = 0; /* 1x */
    camera_setting_data->af_operation_mode = CAM_AF_OFF;
    camera_setting_data->af_range = CAM_AF_RANGE_AUTO;
    camera_setting_data->sharpness = CAM_ADJUST_MED_LEVEL;
    camera_setting_data->is_binning_mode = FALSE;
    camera_setting_data->brightness = CAM_ADJUST_MED_LEVEL;
    camera_setting_data->saturation = CAM_ADJUST_MED_LEVEL;
    camera_setting_data->contrast = CAM_ADJUST_MED_LEVEL;  
#else
    camera_setting_data->zoom = 10; /* 1x */
    camera_setting_data->af_mode = MDI_CAMERA_AUTOFOCUS_MODE_AUTO;
    camera_setting_data->af_metering_mode = MDI_CAMERA_AUTOFOCUS_1_ZONE;
    camera_setting_data->brightness = 128;
    camera_setting_data->saturation = 128;
    camera_setting_data->contrast = 128;
#endif
    camera_setting_data->shutter_pri = 0;
    camera_setting_data->aperture_pri = 0;

    /* default effect */
    camera_setting_data->effect = MDI_CAMERA_EFFECT_NOMRAL;
    camera_setting_data->hue = 0;

    camera_setting_data->overlay_frame_mode = FALSE;
    camera_setting_data->overlay_frame_depth = 0;
    camera_setting_data->overlay_frame_width = 0;
    camera_setting_data->overlay_frame_height = 0;
    camera_setting_data->overlay_frame_source_key = 0;
    camera_setting_data->overlay_frame_buffer_address = 0;

    /* capture parameters */
    camera_setting_data->image_qty = MDI_CAMERA_JPG_QTY_NORMAL;
    camera_setting_data->continue_capture = FALSE;

#ifdef AF_SUPPORT
    camera_setting_data->af_zone0_x = 0;
    camera_setting_data->af_zone0_y = 0;
    camera_setting_data->af_zone1_x = 0;
    camera_setting_data->af_zone1_y = 0;
    camera_setting_data->af_zone2_x = 0;
    camera_setting_data->af_zone2_y = 0;
    camera_setting_data->af_zone3_x = 0;
    camera_setting_data->af_zone3_y = 0;
    camera_setting_data->af_zone4_x = 0;
    camera_setting_data->af_zone4_y = 0;
#endif /* AF_SUPPORT */ 

}

#if defined(BARCODE_SUPPORT)
/*****************************************************************************
 * FUNCTION
 *  mdi_camera_barcodereader_preview_start
 * DESCRIPTION
 *  start barcode preview
 * PARAMETERS
 *  preview_layer_handle        [IN]        Layer for preview
 *  preview_wnd_offset_x        [IN]        Window offset x
 *  preview_wnd_offset_y        [IN]        Window offset y
 *  preview_wnd_width           [IN]        Window width
 *  preview_wnd_height          [IN]        Window height
 *  blt_layer_flag              [IN]        Blt out layer flag
 *  preview_layer_flag          [IN]        Preview layer flag
 *  src_key_color               [IN]        Source key color
 *  is_tvout                    [IN]        Display preview on tvout or not
 *  setting_p                   [IN]        Setting data
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *  MDI_RES_CAMERA_ERR_FAILED   Failed
 *****************************************************************************/
MDI_RESULT mdi_camera_barcodereader_preview_start(
            gdi_handle preview_layer_handle,
            S32 preview_wnd_offset_x,
            S32 preview_wnd_offset_y,
            U16 preview_wnd_width,
            /* preview window width */ U16 preview_wnd_height,
            /* preview window height */ U32 blt_layer_flag,
            U32 preview_layer_flag,
            gdi_color src_key_color,
            BOOL is_tvout,
            mdi_camera_setting_struct *setting_p)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 ret;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* arg check */
    ASSERT(blt_layer_flag > 0);
    ASSERT(preview_layer_flag > 0);

    g_mdi_camera_cntx.is_tvout = is_tvout;

    /* set preview wnd parameters */
    camera_preview_p->preview_layer_handle = preview_layer_handle;
    camera_preview_p->preview_wnd_offset_x = preview_wnd_offset_x;
    camera_preview_p->preview_wnd_offset_y = preview_wnd_offset_y;
    camera_preview_p->preview_wnd_width = preview_wnd_width;
    camera_preview_p->preview_wnd_height = preview_wnd_height;

    camera_preview_p->blt_layer_flag = blt_layer_flag;
    camera_preview_p->preview_layer_flag = preview_layer_flag;

    /* 
     * this parametes only needs for MT6218 with backend, for MT6218,
     * we only support 16 bit color depth, so, U16 is enoguth 
     */
    camera_preview_p->src_key_color = (U16) src_key_color;

    /* init camera parameters */
    memcpy(camera_setting_p, setting_p, sizeof(mdi_camera_setting_struct));

    if (mdi_camera_p->state == CAMERA_STATE_IDLE)
    {
        ret = mdi_camera_barcodereader_preview_start_internal();

        if (ret >= 0)
        {
            /* preview successfully */
            mdi_camera_p->state = CAMERA_STATE_PREVIEWING;
        }

        return ret;
    }
    else
    {
        /* start preview at wrong state */
    #ifndef __MTK_TARGET__  /* MNT Simulator */

        /* on MNT, direct call internal function for debugging */
        mdi_camera_barcodereader_preview_start_internal();
        return MDI_RES_CAMERA_SUCCEED;

    #else /* __MTK_TARGET__ */ 
        /* Target */
        return MDI_RES_CAMERA_ERR_FAILED;
    #endif /* __MTK_TARGET__ */ 
    }
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_barcodereader_preview_start_internal
 * DESCRIPTION
 *  init MDI Camera
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RESULT
 *****************************************************************************/
static MDI_RESULT mdi_camera_barcodereader_preview_start_internal(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_preview_req_struct cam_preview_data;
    MDI_RESULT ret;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    /* fillin preview data from camera_preview_p and camera_setting_p global vars */
    //mdi_camera_fillin_preview_data_v2(&cam_preview_data);
    mdi_camera_fillin_preview_data(&cam_preview_data);
    mdi_camera_p->is_preview_stopped = FALSE;

#ifdef __MTK_TARGET__
    ret = media_cam_barcode_preview(stack_int_get_active_module_id(), &cam_preview_data);

    if (ret != MED_RES_OK)
    {
        return MDI_RES_CAMERA_ERR_HW_NOT_READY;
    }
    else
    {
        mdi_camera_p->state = CAMERA_STATE_PREVIEWING;
        return MDI_RES_CAMERA_SUCCEED;
    }
#else /* __MTK_TARGET__ */ 
    ret = MED_RES_OK;
    return ret;
#endif /* __MTK_TARGET__ */ 

}
#endif /* defined(BARCODE_SUPPORT) */


#if defined(BCR_SUPPORT)
/*****************************************************************************
 * FUNCTION
 *  mdi_camera_bcr_get_recognize_time_duration
 * DESCRIPTION
 *  Get BCR recognize time for last bcr process
 * PARAMETERS
 *  time        [OUT]       Recognizing time for last bcr process
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_bcr_get_recognize_time_duration(U32 *time)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
     media_cam_get_recog_time_duraton(time);
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_bcr_preview_stop
 * DESCRIPTION
 *  Stop BCR preview
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *****************************************************************************/
MDI_RESULT mdi_camera_bcr_preview_stop(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    if (mdi_camera_p->is_preview_stopped == FALSE)
    {
        media_cam_bcr_stop(stack_int_get_active_module_id());
    }

    #ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
    #endif /* __MMI_TVOUT__ */ 

    /* already powered off */
    if (mdi_camera_p->state == CAMERA_STATE_POWER_OFF)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }

    mdi_camera_p->state = CAMERA_STATE_IDLE;
    mdi_camera_p->is_preview_stopped = TRUE;

    return MDI_RES_CAMERA_SUCCEED;

#else /* __MTK_TARGET__ */ 
    /* avoid compile warning in MNT */
    return MDI_RES_CAMERA_SUCCEED;
#endif /* __MTK_TARGET__ */ 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_bcr_capture_image
 * DESCRIPTION
 *  Capture image for bcr recognition 
 * PARAMETERS
 *  buf_pp          [OUT]       Captured image buffer address
 *  image_width     [IN]        Capture image width
 *  image_height    [IN]        Capture image height
 * RETURNS
 *  MED_RES_OK      Success
 *****************************************************************************/
MDI_RESULT mdi_camera_bcr_capture_image(U8 **buf_pp, U16 image_width, U16 image_height)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_bcr_capture_req_struct capture_req;
    MDI_RESULT result;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    capture_req.capture_buffer_p = (void **)buf_pp;
    capture_req.image_width = image_width;
    capture_req.image_height = image_height;

    result = media_cam_bcr_capture(stack_int_get_active_module_id(), &capture_req);
    mdi_camera_p->state = CAMERA_STATE_IDLE;

#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 
    
#endif /* __MTK_TARGET__ */ 

    return MED_RES_OK;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_bcr_clear_image
 * DESCRIPTION
 *  Clear the captured image 
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_bcr_clear_image(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_bcr_clear_raw_data(stack_int_get_active_module_id());
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_bcr_clear_recognized_data
 * DESCRIPTION
 *  Clear recognized data
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_bcr_clear_recognized_data(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_bcr_clear_recog_data(stack_int_get_active_module_id());
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_bcr_recognize
 * DESCRIPTION
 *  Start BCR recognizing process
 * PARAMETERS
 *  handler         [IN]        Callback handler 
 *  mode            [IN]        Bcr mode 
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_bcr_recognize(mdi_bcr_callback handler, S32 mode)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    BcrCurrentHandler = handler;
    SetProtocolEventHandler(mdi_camera_bcr_recognize_result_ind_hdlr, MSG_ID_MEDIA_CAM_BCR_RECOG_RESULT_IND);
#ifdef __MTK_TARGET__
    media_cam_bcr_recognize(stack_int_get_active_module_id(),mode);
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_bcr_abort_recognize
 * DESCRIPTION
 *  Abort BCR recognizing process
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
MDI_RESULT mdi_camera_bcr_abort_recognize(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_bcr_abort_recognizing(stack_int_get_active_module_id());
#endif
    return MDI_RES_CAMERA_SUCCEED;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_bcr_recognize_result_ind_hdlr
 * DESCRIPTION
 *  BCR recognizing result indication handler
 * PARAMETERS
 *  msg_ptr         [IN]    Result struct (media_cam_bcr_recog_result_ind_struct)
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_bcr_recognize_result_ind_hdlr(void *msg_ptr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_bcr_recog_result_ind_struct *ind_p;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ind_p = (media_cam_bcr_recog_result_ind_struct*) msg_ptr;
    
#ifdef __MTK_TARGET__
    if (ind_p->result == MED_RES_OK) 
    {
        BcrCurrentHandler(MDI_RES_CAMERA_SUCCEED,ind_p->recog_number,
                          (mdi_camera_bcr_recog_info_struct*)ind_p->recog_info);
    }
    else
        BcrCurrentHandler(MDI_RES_CAMERA_ERR_FAILED,0,0);
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_bcr_set_raw
 * DESCRIPTION
 *  Set raw data for bcr engine to decode.
 * PARAMETERS
 *  raw_buffer      [IN]        Raw data buffer address
 *  raw_width       [IN]        Raw data width        
 *  raw_height      [IN]        Raw data height
 *  bpp             [IN]        Byte per pixel
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_bcr_set_raw(S8* raw_buffer, U16 raw_width, U16 raw_height, U16 bpp)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_bcr_set_raw_req_struct set_raw_req;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

#ifdef __MTK_TARGET__
    set_raw_req.raw_buffer = (void*) raw_buffer;
    set_raw_req.image_width = raw_width;
    set_raw_req.image_height = raw_height;

    media_cam_bcr_set_raw(stack_int_get_active_module_id(), &set_raw_req);
#endif
}

#endif /* defined(BCR_SUPPORT) */


#if defined(BARCODE_SUPPORT)
/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_and_decode_barcode
 * DESCRIPTION
 *  This function will let camera keep preview and do capturing.
 *  DRV will preview and capture at the same time and
 *  try to decode barcode info from the raw image data.
 * PARAMETERS
 *  text_buf            [IN]        Text buffer address     
 *  buf_length          [IN]        Text buffer length
 *  handler             [IN]        Barcode callback function
 *  barcode_type        [IN]        Barcode type
 * RETURNS
 *  MED_RES_OK          Success
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_and_decode_barcode(PS8 text_buf, S32 buf_length, mdi_barcode_callback handler, U8 barcode_type)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    media_cam_barcode_capture_struct capture_req;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    capture_req.text_buf = text_buf;
    capture_req.buf_length = buf_length;
    capture_req.barcode_type = barcode_type;

    media_cam_barcode_start_capture_end_decode(stack_int_get_active_module_id(), &capture_req);
    barcodeCurrentHandler = handler;
    SetProtocolEventHandler(mdi_camera_barcode_decode_finish_ind, MSG_ID_MEDIA_CAM_BARCODE_DECODE_DONE_IND);
    mdi_camera_p->state = CAMERA_STATE_BARCODE_CAPTURING;

#endif /* __MTK_TARGET__ */ 

    return MED_RES_OK;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_barcode_decode_finish_ind
 * DESCRIPTION
 *  
 * PARAMETERS
 *  inMsg       [?]     
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_barcode_decode_finish_ind(void *inMsg)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_barcode_event_ind_struct *msg_p = (media_cam_barcode_event_ind_struct *) inMsg;
    mdi_result return_code;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (barcodeCurrentHandler && mdi_camera_p->state != CAMERA_STATE_POWER_OFF)
    {
        /* after captured. back to idle state */
        mdi_camera_p->state = CAMERA_STATE_IDLE;
        mdi_camera_p->is_preview_stopped = TRUE;

        switch (msg_p->cause)
        {
            case MED_RES_BR_ERROR_BARCODE_TYPE:
                return_code = MDI_RES_BARCODEREADER_ERROR_CODE_TYPE;
                break;
            case MED_RES_BR_DECODE_TIMEOUT:
                return_code = MDI_RES_BARCODEREADER_DECODE_TIMEOUT;
                break;
            case MED_RES_BR_IMAGE_ERROR:
                return_code = MDI_RES_BARCODEREADER_IMAGE_ERROR;
                break;                
            case MED_RES_BR_MODULE_ERROR:
                return_code = MDI_RES_BARCODEREADER_MODULE_ERROR;
                break;                
            case MED_RES_BR_OUT_OF_MEMORY:
                return_code = MDI_RES_BARCODEREADER_OUT_OF_MEMORY;
                break;                
            case MED_RES_BR_OVER_VERSION:
                return_code = MDI_RES_BARCODEREADER_OVER_VERSION;
                break;                
            case MED_RES_OK:
            default:
                return_code = MDI_RES_BARCODEREADER_DECODE_OK;
                break;
        }
        barcodeCurrentHandler(return_code,msg_p->encoding,msg_p->nsymbol,msg_p->symbolid,msg_p->paritydata,msg_p->barcode_type);
    }
#endif /* __MTK_TARGET__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_stop_capture_and_decode_barcode
 * DESCRIPTION
 *  This function will make camera leave preview and stop capturing.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_stop_capture_and_decode_barcode(void)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state == CAMERA_STATE_BARCODE_CAPTURING)
    {
        media_cam_barcode_stop_capture_and_decode(stack_int_get_active_module_id());
        mdi_camera_p->state = CAMERA_STATE_PREVIEWING;
    }
#endif /* __MTK_TARGET__ */ 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_barcodereader_preview_stop
 * DESCRIPTION
 *  Stop barcode preview
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *****************************************************************************/
MDI_RESULT mdi_camera_barcodereader_preview_stop(void)
{
#ifdef __MTK_TARGET__
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->is_preview_stopped == FALSE)
    {
        media_cam_barcode_stop(stack_int_get_active_module_id());
    }

#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    /* already powered off */
    if (mdi_camera_p->state == CAMERA_STATE_POWER_OFF)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }

    mdi_camera_p->state = CAMERA_STATE_IDLE;
    mdi_camera_p->is_preview_stopped = TRUE;

    return MDI_RES_CAMERA_SUCCEED;

#else /* __MTK_TARGET__ */ 
    /* avoid compile warning in MNT */
    return MDI_RES_CAMERA_SUCCEED;
#endif /* __MTK_TARGET__ */ 
}
#endif /* BARCODE_SUPPORT */


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_flash_setting
 * DESCRIPTION
 *  Turn on/off LED flash light
 * PARAMETERS
 *  is_on       [IN]    On/off 
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_flash_setting(BOOL is_on)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MTK_TARGET__
    media_cam_flash_setting(stack_int_get_active_module_id(), is_on);
#endif 

}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_blocked_recovery
 * DESCRIPTION
 *  This function will be called by mdi_switch_device_ownership when someone
 *  switch device ownership.
 *  When switching ownership, we will force media task to termiate all action
 *  and back to idle. MDI also need restore state to idle.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_blocked_recovery(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __MMI_TVOUT__
    if (mdi_tvout_is_enable())
    {
        if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
        {
            mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
        }
    }
#endif /* __MMI_TVOUT__ */ 
#ifdef __MTK_TARGET__
    mdi_camera_p->state = CAMERA_STATE_POWER_OFF;
#endif 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_save_captured_image
 * DESCRIPTION
 *  According to the passed file name, MED saves the capture result to the file
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Failed
 *****************************************************************************/
S32 mdi_camera_save_captured_image(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 result;
    result = media_cam_save_image(stack_int_get_active_module_id());

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }
}



/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_to_memory_bracketing
 * DESCRIPTION
 *  EV bracketing capture and this is a blocking function for 6238 series
 * PARAMETERS
 *  jpgs_ptr    [IN]    The pointer for storing captured jpeg info structure 
 *  buffer      [IN]    The buffer address
 *  buffer_size [IN]    The buffer size
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_to_memory_bracketing(mdi_camera_jpegs_struct *jpgs_ptr,U8* buffer, S32 buffer_size)
{
#if (MDI_CAMERA_MT6238_SERIES)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MMI_BOOL tvout_need_to_sleep_out = MMI_FALSE;
#endif /* __MMI_TVOUT__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }

    capture_data.image_width = camera_setting_p->image_width;
    capture_data.image_height = camera_setting_p->image_height;

    capture_data.media_mode = MED_MODE_ARRAY;
    capture_data.source_device = CAM_SRC_ISP;   /* ISP */
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.capture_buffer_p = NULL;
    capture_data.data = (void*)buffer;                   /* not used */
    capture_data.file_buffer_len = buffer_size;           /* not used */
    capture_data.file_size_p = 0;
    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.snapshot_number = 3;
    capture_data.gray_mode = 0;

    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_BRACKET_EXPOSURE_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_pitch_mode = FALSE;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */

    capture_data.display = FALSE;    /* diplay to LCM */
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.lcm_start_x = g_mdi_camera_cntx.lcm_start_x;
    capture_data.lcm_start_y = g_mdi_camera_cntx.lcm_start_y;
    capture_data.lcm_end_x = g_mdi_camera_cntx.lcm_end_x;
    capture_data.lcm_end_y = g_mdi_camera_cntx.lcm_end_y;
    capture_data.update_layer = g_mdi_camera_cntx.update_layer;
    capture_data.hw_update_layer = g_mdi_camera_cntx.hw_update_layer;
    capture_data.frame_buffer_address = (kal_uint32) g_mdi_camera_cntx.frame_buf_ptr;
    capture_data.frame_buffer_size = g_mdi_camera_cntx.frame_buf_size;
    capture_data.roi_offset_x = g_mdi_camera_cntx.roi_offset_x;
    capture_data.roi_offset_y = g_mdi_camera_cntx.roi_offset_y;


    mmi_camera_fill_overlay_data(&capture_data);

    capture_data.tv_output = FALSE;
#ifdef __MTK_TARGET__
    capture_data.jpegs_p = (media_jpegs_struct*) jpgs_ptr;
#endif 


#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
    {
        mdi_tvout_sleep_in();
        tvout_need_to_sleep_out = MMI_TRUE;
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && tvout_need_to_sleep_out == MMI_TRUE)
    {
        mdi_tvout_sleep_out();
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }

#else
    /* Only support on MT6238 */
    ASSERT(0);
    return MDI_RES_CAMERA_ERR_FAILED;
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_to_file_continuous_shot
 * DESCRIPTION
 *  EV bracketing capture and this is a blocking function for 6238 series
 * PARAMETERS
 *  file_name       [IN]    The pointer of filename buffer array
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_to_file_continuous_shot(S8* file_name)
{
#if (MDI_CAMERA_MT6238_SERIES)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MMI_BOOL tvout_need_to_sleep_out = MMI_FALSE;
#endif /* __MMI_TVOUT__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if MDI_CAMERA_MT6225_SERIES
    capture_data.memory_output = FALSE;
#endif
    capture_data.image_width = camera_setting_p->image_width;
    capture_data.image_height = camera_setting_p->image_height;

    capture_data.media_mode = MED_MODE_FILE;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_ISP;   /* ISP */
    capture_data.data = (void*)file_name;
    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.snapshot_number = 1;
    capture_data.gray_mode = 0;
    capture_data.continue_capture = TRUE;
    capture_data.capture_buffer_p = NULL;
    capture_data.file_size_p = NULL;

    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_NORMAL_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_pitch_mode = FALSE;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */
    mmi_camera_fill_overlay_data(&capture_data);
    capture_data.display = TRUE;
    capture_data.tv_output = FALSE;

    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.lcm_start_x = g_mdi_camera_cntx.lcm_start_x;
    capture_data.lcm_start_y = g_mdi_camera_cntx.lcm_start_y;
    capture_data.lcm_end_x = g_mdi_camera_cntx.lcm_end_x;
    capture_data.lcm_end_y = g_mdi_camera_cntx.lcm_end_y;
    capture_data.update_layer = g_mdi_camera_cntx.update_layer;
    capture_data.hw_update_layer = g_mdi_camera_cntx.hw_update_layer;
    capture_data.frame_buffer_address = (kal_uint32) g_mdi_camera_cntx.frame_buf_ptr;
    capture_data.frame_buffer_size = g_mdi_camera_cntx.frame_buf_size;
    capture_data.roi_offset_x = g_mdi_camera_cntx.roi_offset_x;
    capture_data.roi_offset_y = g_mdi_camera_cntx.roi_offset_y;

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
    {
        mdi_tvout_sleep_in();
        tvout_need_to_sleep_out = MMI_TRUE;
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && tvout_need_to_sleep_out == MMI_TRUE)
    {
        mdi_tvout_sleep_out();
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }
#else
    /* Only support on MT6238 */
    ASSERT(0);
    return MDI_RES_CAMERA_ERR_FAILED;
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_to_memory_best_shot
 * DESCRIPTION
 *  EV bracketing capture for 6238 series and this is a blocking function
 * PARAMETERS
 *  buf_pp          [OUT]       The pointer to pointer for get capture buffer address
 *  captured_size   [OUT]       The pointer of capture size
 *  buffer          [IN]        The buffer address
 *  buffer_size     [IN]        The buffer size
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_to_memory_best_shot(U8** buf_pp, U32 *captured_size, U8* buffer, S32 buffer_size)
{
#if (MDI_CAMERA_MT6238_SERIES)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MMI_BOOL tvout_need_to_sleep_out = MMI_FALSE;
#endif /* __MMI_TVOUT__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }
#if MDI_CAMERA_MT6225_SERIES
    capture_data.memory_output = FALSE;
#endif
    capture_data.image_width = camera_setting_p->image_width;
    capture_data.image_height = camera_setting_p->image_height;

    capture_data.media_mode = MED_MODE_ARRAY;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.capture_buffer_p = (void **)buf_pp;
    capture_data.file_size_p = (kal_uint32*) captured_size;
    capture_data.source_device = CAM_SRC_ISP;   /* ISP */
    capture_data.data = (void*) buffer;               /* not used */
    capture_data.file_buffer_len = buffer_size;       /* not used */
    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.snapshot_number = 3;
    capture_data.gray_mode = 0;
    capture_data.continue_capture = FALSE;

    capture_data.display = FALSE;    /* diplay to LCM */
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.lcm_start_x = g_mdi_camera_cntx.lcm_start_x;
    capture_data.lcm_start_y = g_mdi_camera_cntx.lcm_start_y;
    capture_data.lcm_end_x = g_mdi_camera_cntx.lcm_end_x;
    capture_data.lcm_end_y = g_mdi_camera_cntx.lcm_end_y;
    capture_data.update_layer = g_mdi_camera_cntx.update_layer;
    capture_data.hw_update_layer = g_mdi_camera_cntx.hw_update_layer;
    capture_data.frame_buffer_address = (kal_uint32) g_mdi_camera_cntx.frame_buf_ptr;
    capture_data.frame_buffer_size = g_mdi_camera_cntx.frame_buf_size;
    capture_data.roi_offset_x = g_mdi_camera_cntx.roi_offset_x;
    capture_data.roi_offset_y = g_mdi_camera_cntx.roi_offset_y;

    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_BEST_SHOT_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_pitch_mode = FALSE;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */
    mmi_camera_fill_overlay_data(&capture_data);
    capture_data.tv_output = FALSE;
#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
    {
        mdi_tvout_sleep_in();
        tvout_need_to_sleep_out = MMI_TRUE;
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && tvout_need_to_sleep_out == MMI_TRUE)
    {
        mdi_tvout_sleep_out();
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }
#else
    /* Only support on MT6238 */
    ASSERT(0);
    return MDI_RES_CAMERA_ERR_FAILED;
#endif
}



/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_to_memory_burst_shot
 * DESCRIPTION
 *  Non-blocking burstshot capture for 6238 series
 * PARAMETERS
 *  capture_count       [IN]        Capture number
 *  jpgs_ptr            [OUT]       Captured jpeg info        
 *  callback            [IN]        Capture result callback
 *  buffer              [IN]        Burstshot buffer address
 *  buffer_size         [IN]        Burstshot buffer length
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_to_memory_burst_shot(U32 capture_count, 
                                                   mdi_camera_jpegs_struct *jpgs_ptr, 
                                                   capture_result_callback callback, 
                                                   U8* buffer, 
                                                   S32 buffer_size)
{
#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MDI_RESULT ret;
#endif
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }

    ASSERT(callback != NULL);

    g_mdi_camera_cntx.burst_cap_count = capture_count;

    mdi_camera_non_block_burst_shot_result_callback = callback;
    SetProtocolEventHandler(mdi_camera_non_block_burst_capture_result_hdlr, MSG_ID_MEDIA_CAM_CAPTURE_EVENT_IND);

    capture_data.image_width = camera_setting_p->image_width;
    capture_data.image_height = camera_setting_p->image_height;
    capture_data.media_mode = MED_MODE_ARRAY;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_ISP;   /* ISP */

    capture_data.data = (void*) buffer;               /* not used */
    capture_data.file_buffer_len = buffer_size;       /* not used */
    capture_data.file_size_p = NULL;        /* not used */
    capture_data.capture_buffer_p = NULL;   /* not used */

    capture_data.snapshot_number = capture_count;
    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.gray_mode = 0;
    capture_data.continue_capture = TRUE;
#ifdef __MTK_TARGET__
    capture_data.jpegs_p = (media_jpegs_struct*) jpgs_ptr;
#endif 

#if (MDI_CAMERA_MT6238_SERIES)
    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_BURST_SHOT_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */
#elif (MDI_CAMERA_MT6228_SERIES)
    capture_data.manual_wb = FALSE;
    capture_data.thumbnail_mode = FALSE;
    #ifdef __MTK_TARGET__
    capture_data.image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif
#endif

    capture_data.image_pitch_mode = FALSE;

    capture_data.display = FALSE;    /* diplay to LCM */
    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.lcm_start_x = g_mdi_camera_cntx.lcm_start_x;
    capture_data.lcm_start_y = g_mdi_camera_cntx.lcm_start_y;
    capture_data.lcm_end_x = g_mdi_camera_cntx.lcm_end_x;
    capture_data.lcm_end_y = g_mdi_camera_cntx.lcm_end_y;
    capture_data.update_layer = g_mdi_camera_cntx.update_layer;
    capture_data.hw_update_layer = g_mdi_camera_cntx.hw_update_layer;
    capture_data.frame_buffer_address = (kal_uint32) g_mdi_camera_cntx.frame_buf_ptr;
    capture_data.frame_buffer_size = g_mdi_camera_cntx.frame_buf_size;
    capture_data.roi_offset_x = g_mdi_camera_cntx.roi_offset_x;
    capture_data.roi_offset_y = g_mdi_camera_cntx.roi_offset_y;

    mmi_camera_fill_overlay_data(&capture_data);

    capture_data.tv_output = FALSE;

    /* TV out */
#ifdef __MMI_TVOUT__

    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            /* set tvout owner to Camera */
            ret = mdi_tvout_get_para(
                    MDI_TV_OWNER_CAMERA,
                    NULL,                                   /* mode_ptr */
                    (PU16) & capture_data.tv_output_width,  /* buf_width_ptr */
                    (PU16) & capture_data.tv_output_height, /* buf_height_ptr */
                    (PU32) & capture_data.tv_output_buffer_size,       /* buf size */
                    (PU8*) & capture_data.tv_output_buffer1_address,  /* buf1 pp */
                    (PU8*) & capture_data.tv_output_buffer2_address,  /* buf2 pp */
                    NULL,                                   /* rotate_buf_1_pp */
                    NULL);                                  /* rotate_buf_2_pp */

            if (ret == MDI_RES_TV_ENABLE)
            {
                capture_data.tv_output = TRUE;
                capture_data.tv_output_offset_x = g_mdi_camera_cntx.tv_output_offset_x;
                capture_data.tv_output_offset_y = g_mdi_camera_cntx.tv_output_offset_y;
            }

        }   /* tvout is enable or not */
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

#if (MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES) 
    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }
#endif

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_NOT_ENOUGH_SPACE)
    {
        return MDI_RES_CAMERA_ERR_MEMORY_NOT_ENOUGH;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }
#else /* MDI_CAMERA_MT6228_SERIES */ 
    /* only availabe on MT6228 */
    ASSERT(0);
    return MDI_RES_CAMERA_ERR_FAILED;
#endif /* MDI_CAMERA_MT6228_SERIES */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_focus_zone
 * DESCRIPTION
 *  Get camera driver AF zone location for 6238 series and 
 *  the location coordinate is the preview layer not LCD 
 * PARAMETERS
 *  zone            [OUT]       AF zone structure        
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_get_focus_zone(mdi_camera_af_window_struct* zone)
{
#if (MDI_CAMERA_MT6238_SERIES)
#ifdef __MTK_TARGET__
    {
        media_cam_af_zone_para_struct af_zone;
        U8 i = 0;
        media_cam_get_focus_zone(stack_int_get_active_module_id(), &af_zone);

        zone->af_active_zone = af_zone.af_active_zone;

        for (;i<MDI_CAMERA_AF_ZONE_MAX_NO; i++)
        {
            zone->af_zone[i].af_zone_h = af_zone.af_zone[i].af_zone_h;
            zone->af_zone[i].af_zone_w = af_zone.af_zone[i].af_zone_w;
            zone->af_zone[i].af_zone_x = af_zone.af_zone[i].af_zone_x;
            zone->af_zone[i].af_zone_y = af_zone.af_zone[i].af_zone_y;
        }
    }
#endif 

#else
    /* Only support on MT6238 */
    ASSERT(0);
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_af_range
 * DESCRIPTION
 *  Update af range value for 6238 series camera
 * PARAMETERS
 *  cam_para        [IN]        AF range value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_af_range(U16 cam_para)
{
#if (MDI_CAMERA_MT6238_SERIES)
    camera_setting_p->af_range = cam_para;
#ifdef __MTK_TARGET__
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_AF_RANGE, cam_para);
    }
#endif 
#else
    /* Only support on MT6238 */
    ASSERT(0);
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_af_operation_mode
 * DESCRIPTION
 *  Update af operation mode value for 6238 series camera
 * PARAMETERS
 *  cam_para        [IN]        AF range value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_af_operation_mode(U16 cam_para)
{
#if (MDI_CAMERA_MT6238_SERIES)
    camera_setting_p->af_operation_mode = cam_para;
#ifdef __MTK_TARGET__
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_AF_OPERATION, cam_para);
    }
#endif 

#else
    /* Only support on MT6238 */
    ASSERT(0);
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_update_para_sharpness
 * DESCRIPTION
 *  Update sharpness setting value for 6238 series camera
 * PARAMETERS
 *  cam_para        [IN]        Sharpness setting value
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_update_para_sharpness(U16 cam_para)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if (MDI_CAMERA_MT6238_SERIES)
    camera_setting_p->sharpness = cam_para;

#ifdef __MTK_TARGET__
        media_cam_set_param(stack_int_get_active_module_id(), CAM_PARAM_SHARPNESS, cam_para);
#endif /* __MTK_TARGET__ */ 
#else
    /* Only support on MT6238 */
    ASSERT(0);
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mmi_camera_update_overlay_data
 * DESCRIPTION
 *  Update overlay struct for 6238 series
 * PARAMETERS
 *  overlay        [IN]        Overlay structure pointer
 * RETURNS
 *  void
 *****************************************************************************/
void mmi_camera_update_overlay_data(mdi_camera_overlay_struct *overlay)
{
#if (MDI_CAMERA_MT6238_SERIES)

    if (overlay == NULL)
    {
        if (camera_overlay_p != NULL)
        {
            OslMfree((S8*)camera_overlay_p);
            camera_overlay_p = NULL;
        }
        return;
    }

    if (camera_overlay_p == NULL)
    {
        camera_overlay_p = (mdi_camera_overlay_struct*)OslMalloc(sizeof(mdi_camera_overlay_struct));       
    }

    camera_overlay_p->overlay_frame_mode = overlay->overlay_frame_mode;

    camera_overlay_p->overlay_frame_width = overlay->overlay_frame_width;
    camera_overlay_p->overlay_frame_height = overlay->overlay_frame_height;
    camera_overlay_p->overlay_frame_buffer_address = overlay->overlay_frame_buffer_address;
    camera_overlay_p->overlay_frame_source_key = overlay->overlay_frame_source_key;
    camera_overlay_p->overlay_frame_depth = overlay->overlay_frame_depth;
    camera_overlay_p->overlay_palette_addr = overlay->overlay_palette_addr;
    camera_overlay_p->overlay_palette_size = overlay->overlay_palette_size;
#else
    /* Only support on MT6238 */
    ASSERT(0);
#endif

}
#if MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES
static void mmi_camera_fill_overlay_data(media_cam_capture_req_struct* capture_data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (camera_overlay_p == NULL)
    {
        capture_data->overlay_frame_mode = camera_setting_p->overlay_frame_mode;
        capture_data->overlay_color_depth = camera_setting_p->overlay_frame_depth;
        capture_data->overlay_frame_source_key = camera_setting_p->overlay_frame_source_key;
        capture_data->overlay_frame_width = camera_setting_p->overlay_frame_width;
        capture_data->overlay_frame_height = camera_setting_p->overlay_frame_height;
        capture_data->overlay_frame_buffer_address = camera_setting_p->overlay_frame_buffer_address;
        capture_data->overlay_palette_addr = camera_setting_p->overlay_palette_addr;
        capture_data->overlay_palette_size = camera_setting_p->overlay_palette_size;
    }
    else
    {
        capture_data->overlay_frame_mode = camera_overlay_p->overlay_frame_mode;
        capture_data->overlay_color_depth = camera_overlay_p->overlay_frame_depth;
        capture_data->overlay_frame_source_key = camera_overlay_p->overlay_frame_source_key;
        capture_data->overlay_frame_width = camera_overlay_p->overlay_frame_width;
        capture_data->overlay_frame_height = camera_overlay_p->overlay_frame_height;
        capture_data->overlay_frame_buffer_address = camera_overlay_p->overlay_frame_buffer_address;
        capture_data->overlay_palette_addr = camera_overlay_p->overlay_palette_addr;
        capture_data->overlay_palette_size = camera_overlay_p->overlay_palette_size;
    }
}
#endif /* MDI_CAMERA_MT6228_SERIES || MDI_CAMERA_MT6238_SERIES */


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_set_camera_id
 * DESCRIPTION
 *  Choose the camera sensor
 * PARAMETERS
 *  camera_id       [IN]    Camera sensor id [main or sub]
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *  MDI_RES_CAMERA_ERR_FAILED   Failure
 *****************************************************************************/
MDI_RESULT mdi_camera_set_camera_id(U16 camera_id)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MDI_RESULT ret;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if MDI_CAMERA_MT6238_SERIES
    if (mdi_camera_p->state == CAMERA_STATE_POWER_OFF)
    {
        mdi_camera_p->cam_id = camera_id;
        ret = MDI_RES_CAMERA_SUCCEED;
    }
    else
    {
        ret = MDI_RES_CAMERA_ERR_FAILED;
    }
#else
    if (camera_id == MDI_CAMERA_MAIN)
    {
        ret = MDI_RES_CAMERA_SUCCEED;
    }
    else
    {
        ret = MDI_RES_CAMERA_ERR_FAILED;
    }
#endif
    return ret;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_query_support_info
 * DESCRIPTION
 *  Query camera sensor function capability
 * PARAMETERS
 *  mdi_setting_type        [IN]        Function type
 *  p_info                  [OUT]       Function support information structure 
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_query_support_info(mdi_camera_setting_type_enum mdi_setting_type, mdi_camera_fun_info *p_info)
{
#if (MDI_CAMERA_MT6238_SERIES) || (defined(MT6253) || defined(MT6253T) || defined(MT6235) || defined(MT6235B)) 
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    camera_capability_func_struct* camera_capability_func=NULL;
    cam_fun_info cam_info;
    U8 i;
    MDI_RESULT ret;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ret = mdi_camera_detect_sensor_id(mdi_camera_p->cam_id);

    ASSERT(ret == MDI_RES_CAMERA_SUCCEED);
    
    camera_capability_func_config(&camera_capability_func);

    /* Please DRV check if them checked-in sensor capability related information */
    ASSERT(camera_capability_func!=NULL);

    switch (mdi_setting_type)
    {
        case MDI_CAMERA_SETTING_CAPMODE:
            camera_capability_func->get_capture_mode_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_CAMSCENE:
            camera_capability_func->get_camera_scene_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_CAPSIZE:
            camera_capability_func->get_capture_size_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_FLASH:
            camera_capability_func->get_flashlight_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_AFRANGE:
            camera_capability_func->get_af_range_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_ISO:
            camera_capability_func->get_iso_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_BINNING:
            camera_capability_func->get_binning_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_WB:
            camera_capability_func->get_wb_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_EFFECT:
            camera_capability_func->get_effect_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_AFMODE:
            camera_capability_func->get_af_mode_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_AEMETER:
            camera_capability_func->get_ae_meter_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_SHARPNESS:
            camera_capability_func->get_sharpness_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_CONTRAST:
            camera_capability_func->get_contrast_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_SATURATION:
            camera_capability_func->get_saturation_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_EV:
            camera_capability_func->get_ev_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_VIDEOSCENE:
            camera_capability_func->get_video_scene_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_VIDEOCODEC:
            camera_capability_func->get_video_codec_ability(&cam_info);
            break;

        case MDI_CAMERA_SETTING_VIDEOSIZE:
            camera_capability_func->get_video_size_ability(&cam_info);
            break;

        default:
            ASSERT(0);
            break;
    }

    p_info->is_support = cam_info.is_support;
    p_info->item_count = cam_info.item_count;

    ASSERT(MAX_CAMERA_SUPPORT_ITEM_COUNT>=cam_info.item_count);
    
    for (i=0;i<cam_info.item_count;i++)
    {
        p_info->support_item[i] = cam_info.support_item[i];
    }
#endif /* (MDI_CAMERA_MT6238_SERIES) || (defined(MT6253) || defined(MT6253T) || defined(MT6235) || defined(MT6235B)) */
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_query_zoom_info
 * DESCRIPTION
 *  Query the zoom information
 * PARAMETERS
 *  p_info          [OUT]        The current zoom capability
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_query_zoom_info(mdi_camera_zoom_info *p_info)
{
#if (MDI_CAMERA_MT6238_SERIES) || (defined(MT6253) || defined(MT6253T) || defined(MT6235) || defined(MT6235B)) 
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    camera_capability_func_struct* camera_capability_func=NULL;
    cam_zoom_info cam_info;
    MDI_RESULT ret;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ret = mdi_camera_detect_sensor_id(mdi_camera_p->cam_id);

    ASSERT(ret == MDI_RES_CAMERA_SUCCEED);
    
    camera_capability_func_config(&camera_capability_func);

    ASSERT(camera_capability_func!=NULL);
    
    camera_capability_func->get_zoom_ability(&cam_info); 
    p_info->is_support = cam_info.is_support;
    p_info->max_zoom_factor = cam_info.max_zoom_factor;
    p_info->zoom_steps = cam_info.zoom_steps;
#endif /* (MDI_CAMERA_MT6238_SERIES) || (defined(MT6253) || defined(MT6253T) || defined(MT6235) || defined(MT6235B)) */
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_query_imgsize_upperbound
 * DESCRIPTION
 *  Query the maximum capture size based on iso value
 * PARAMETERS
 *  iso_value       [IN]        ISO value
 * RETURNS
 *  image size      The upperbound image size
 *****************************************************************************/
U16 mdi_camera_query_imgsize_upperbound(U16 iso_value)
{
#if (MDI_CAMERA_MT6238_SERIES) || (defined(MT6253) || defined(MT6253T) || defined(MT6235) || defined(MT6235B)) 
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    return camera_iso_imgsize_upperbound(iso_value);
#else
    return 0;
#endif /* (MDI_CAMERA_MT6238_SERIES) || (defined(MT6253) || defined(MT6253T) || defined(MT6235) || defined(MT6235B)) */    
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_start_fd_process
 * DESCRIPTION
 *  Start face detection process
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *  MDI_RES_CAMERA_ERR_FAILED   Failed
 *****************************************************************************/
MDI_RESULT mdi_camera_start_fd_process(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 result = MDI_RES_CAMERA_ERR_FAILED;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if defined(__MDI_CAMERA_FD_SUPPORT__)

#ifdef __MTK_TARGET__
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        result = media_cam_resume_fd(stack_int_get_active_module_id());
    }

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else 
    {
        return MDI_RES_CAMERA_ERR_FAILED;
    }

#endif /* __MTK_TARGET__ */ 

#else /* defined(__MDI_CAMERA_FD_SUPPORT__) */
    return result;
#endif
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_stop_fd_process
 * DESCRIPTION
 *  Stop face detection process
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *  MDI_RES_CAMERA_ERR_FAILED   Failed
 *****************************************************************************/
MDI_RESULT mdi_camera_stop_fd_process(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 ret = MED_RES_OK;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if defined(__MDI_CAMERA_FD_SUPPORT__)

    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        ret = media_cam_pause_fd(stack_int_get_active_module_id());
    }    

#endif

    if (ret == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else 
    {
        return MDI_RES_CAMERA_ERR_FAILED;
    }
}


MDI_RESULT mdi_camera_set_focus_zone_by_fd_id(U32 fd_id)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MDI_RESULT ret = MDI_RES_CAMERA_SUCCEED;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if defined(__MDI_CAMERA_FD_SUPPORT__)
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        ret = media_cam_set_fd_id(stack_int_get_active_module_id(),fd_id);
    }    
#endif
    return ret;
}


MDI_RESULT mdi_camera_unset_fd_focus_zone(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MDI_RESULT ret = MDI_RES_CAMERA_SUCCEED;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if defined(__MDI_CAMERA_FD_SUPPORT__)
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        ret = media_cam_unset_fd_id(stack_int_get_active_module_id());
    }    
#endif
    return ret;
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_get_fd_result
 * DESCRIPTION
 *  Get face detection result
 * PARAMETERS
 *  fd_result_p     [OUT]       Face detection result
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *  MDI_RES_CAMERA_ERR_FAILED   Failed
 *****************************************************************************/
MDI_RESULT mdi_camera_get_fd_result(mdi_camera_fd_result_struct *fd_result_p)
{
#if defined(__MDI_CAMERA_FD_SUPPORT__) && defined(__MTK_TARGET__)
    media_cam_fd_result_struct fd_result;
    U8 i = 0;
    media_cam_get_fd_result(stack_int_get_active_module_id(), &fd_result);

    if (fd_result.face_no == 0)
        fd_result_p->fd_result = MDI_RES_CAMERA_ERR_FAILED;
    else
        fd_result_p->fd_result = MDI_RES_CAMERA_SUCCEED;

    fd_result_p->fd_searched_num = fd_result.face_no;

    for(i=0;i<fd_result_p->fd_searched_num;i++)
    {
        fd_result_p->fd_zone[i].fd_id = fd_result.id[i];
        fd_result_p->fd_zone[i].fd_priority = fd_result.priority[i];
        fd_result_p->fd_zone[i].fd_zone_x = (fd_result.rect_x0[i] + fd_result.rect_x1[i])/2;
        fd_result_p->fd_zone[i].fd_zone_y = (fd_result.rect_y0[i] + fd_result.rect_y1[i])/2;
        fd_result_p->fd_zone[i].fd_zone_w = fd_result.rect_x1[i] - fd_result.rect_x0[i];
        fd_result_p->fd_zone[i].fd_zone_h = fd_result.rect_y1[i] - fd_result.rect_y0[i];;                
    }
#endif /* defined(__MDI_CAMERA_FD_SUPPORT__) && defined(__MTK_TARGET__) */
    return MDI_RES_CAMERA_SUCCEED;

}

/*****************************************************************************
 * FUNCTION
 *  mdi_camera_af_finish_ind
 * DESCRIPTION
 *  
 * PARAMETERS
 *  inMsg       [?]     
 * RETURNS
 *  void
 *****************************************************************************/
void mdi_camera_fd_result_ind(void *inMsg)
{
#if defined(__MDI_CAMERA_FD_SUPPORT__)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_fd_result_ind_struct *msg_p = (media_cam_fd_result_ind_struct*) inMsg;
    mdi_camera_fd_result_ind_struct result_ind;
    U8 i;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state == CAMERA_STATE_PREVIEWING)
    {
        if (g_mdi_camera_fd_callback)
        {
            if (msg_p->fd_result.face_no > 0)
            {
                result_ind.fd_result = MDI_RES_CAMERA_SUCCEED;
                result_ind.fd_searched_num = msg_p->fd_result.face_no;

                for(i=0;i<result_ind.fd_searched_num;i++)
                {
                    result_ind.fd_zone[i].fd_id = msg_p->fd_result.id[i];
                    result_ind.fd_zone[i].fd_priority = msg_p->fd_result.priority[i];
                    result_ind.fd_zone[i].fd_zone_x = (msg_p->fd_result.rect_x0[i] + msg_p->fd_result.rect_x1[i])/2;
                    result_ind.fd_zone[i].fd_zone_y = (msg_p->fd_result.rect_y0[i] + msg_p->fd_result.rect_y1[i])/2;
                    result_ind.fd_zone[i].fd_zone_w = msg_p->fd_result.rect_x1[i] - msg_p->fd_result.rect_x0[i];
                    result_ind.fd_zone[i].fd_zone_h = msg_p->fd_result.rect_y1[i] - msg_p->fd_result.rect_y0[i];;                
                }
            }
            else
            {
                result_ind.fd_result = MDI_RES_CAMERA_ERR_FAILED;
            }

            g_mdi_camera_fd_callback(&result_ind);
        }
        else
        {
            ASSERT(0);
        }
    }
#endif /* __MDI_CAMERA_FD_SUPPORT__ */ 
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_capture_to_memory_panorama
 * DESCRIPTION
 *  Capture one of panorama images
 * PARAMETERS
 *  buffer                      [IN]        Capture buffer address       
 *  buffer_size                 [OUT]       Capture buffer size
 *  captured_buffer_size_p      [IN]        Capture image size 
 *  shot_num                    [IN]        The capture image index
 *  direction                   [IN]        Panorama stitch direction
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED                  Success
 *  MDI_RES_CAMERA_ERR_DISK_FULL            Disk full
 *  MDI_RES_CAMERA_ERR_WRITE_PROTECTION     Disk write protection
 *  MDI_RES_CAMERA_ERR_NO_DISK              No disk
 *  MDI_RES_CAMERA_ERR_CAPTURE_FAILED       Capture failed
 *****************************************************************************/
MDI_RESULT mdi_camera_capture_to_memory_panorama(U8* buffer, 
                                                 U32 buffer_size,
                                                 U32* captured_buffer_size_p,
                                                 U8 shot_num,
                                                 U8 direction)
{
#if defined(__MDI_CAMERA_PANORAMA_VIEW_SUPPORT__)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_capture_req_struct capture_data;
    S32 result;
#ifdef __MMI_TVOUT__
    MMI_BOOL tvout_need_to_sleep_out = MMI_FALSE;
#endif /* __MMI_TVOUT__ */ 

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (mdi_camera_p->state != CAMERA_STATE_PREVIEWING)
    {
        ASSERT(0);
    }

    capture_data.image_width = camera_setting_p->image_width;
    capture_data.image_height = camera_setting_p->image_height;

    capture_data.media_mode = MED_MODE_ARRAY;
    capture_data.image_quality = (U8) camera_setting_p->image_qty;
    capture_data.source_device = CAM_SRC_ISP;   /* ISP */
    capture_data.data = (void*) buffer;
    capture_data.file_buffer_len = buffer_size;
    capture_data.file_size_p = captured_buffer_size_p;
    capture_data.capture_buffer_p = NULL;   /* not used */

    capture_data.flash_mode = (kal_uint16) camera_setting_p->flash;
    capture_data.snapshot_number = 1;
    capture_data.current_shot_number = shot_num;
    capture_data.direction = direction;
    capture_data.gray_mode = 0;
    capture_data.continue_capture = FALSE;

    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.rot_angle = 0;
    capture_data.special_capture_mode = CAM_CAPTURE_PANORAMA_SHOT_MODE;
    capture_data.raw_capture_enable = FALSE;
    capture_data.raw_image_buffer_address = 0;
    capture_data.image_pitch_mode = FALSE;
    capture_data.image_data_format = IMG_COLOR_FMT_RGB565;
    capture_data.image_bg_image_width = 0;
    capture_data.thumbnail_width = 0;  /* 0: disable thumbnail */
    capture_data.thumbnail_height = 0; /* 0: disable thumbnail */

    mmi_camera_fill_overlay_data(&capture_data);

    capture_data.display = FALSE;
    capture_data.tv_output = FALSE;

    capture_data.lcd_id = g_mdi_camera_cntx.lcd_id;
    capture_data.lcm_start_x = g_mdi_camera_cntx.lcm_start_x;
    capture_data.lcm_start_y = g_mdi_camera_cntx.lcm_start_y;
    capture_data.lcm_end_x = g_mdi_camera_cntx.lcm_end_x;
    capture_data.lcm_end_y = g_mdi_camera_cntx.lcm_end_y;
    capture_data.update_layer = g_mdi_camera_cntx.update_layer;
    capture_data.hw_update_layer = g_mdi_camera_cntx.hw_update_layer;
    capture_data.frame_buffer_address = (kal_uint32) g_mdi_camera_cntx.frame_buf_ptr;
    capture_data.frame_buffer_size = g_mdi_camera_cntx.frame_buf_size;
    capture_data.roi_offset_x = g_mdi_camera_cntx.roi_offset_x;
    capture_data.roi_offset_y = g_mdi_camera_cntx.roi_offset_y;
    

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
    {
        mdi_tvout_sleep_in();
        tvout_need_to_sleep_out = MMI_TRUE;
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture jpeg */
    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);

    /* free overlay relate cntx. */
    if (camera_overlay_p != NULL)
    {
        OslMfree((S8*)camera_overlay_p);
        camera_overlay_p = NULL;
    }

#ifdef __MMI_TVOUT__
    /* temp solution for 200M pixtel catpure */
    /* the bandwidth is no enoguth for 200M capture with TV-out enable */
    if (g_mdi_camera_cntx.is_tvout && tvout_need_to_sleep_out == MMI_TRUE)
    {
        mdi_tvout_sleep_out();
    }
#endif /* __MMI_TVOUT__ */ 

    /* capture will also stop preview */
    mdi_camera_p->is_preview_stopped = TRUE;

    /* after captured. back to idle state */
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    /* capture will also stop preview TV-Out */
#ifdef __MMI_TVOUT__
    /* restore owner to GDI */
    if (g_mdi_camera_cntx.is_tvout)
    {
        if (mdi_tvout_is_enable())
        {
            if (g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
            {
                mdi_tvout_set_owner(MDI_TV_OWNER_GDI);
            }
        }
    }
#endif /* __MMI_TVOUT__ */ 

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else if (result == MED_RES_DISC_FULL)
    {
        return MDI_RES_CAMERA_ERR_DISK_FULL;
    }
    else if (result == MED_RES_WRITE_PROTECTION)
    {
        return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
    }
    else if (result == MED_RES_NO_DISC)
    {
        return MDI_RES_CAMERA_ERR_NO_DISK;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_CAPTURE_FAILED;
    }
#else /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
    /* not supported on !MT6238_SERIES */
    ASSERT(0);

#endif /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_pause_stitching_panorama
 * DESCRIPTION
 *  Pause stitching panorama process
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *  MDI_RES_CAMERA_ERR_FAILED   Failed
 *****************************************************************************/
MDI_RESULT mdi_camera_pause_stitching_panorama(void)
{
#if defined(__MDI_CAMERA_PANORAMA_VIEW_SUPPORT__)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MDI_RESULT result;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    result = media_cam_pause_panorama(stack_int_get_active_module_id());

    if (result == MED_RES_OK)
    {
        mdi_camera_p->state = CAMERA_STATE_IDLE;
        return MDI_RES_CAMERA_SUCCEED;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_FAILED;
    }

#else /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
    return MDI_RES_CAMERA_ERR_FAILED;
#endif /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_resume_stitching_panorama
 * DESCRIPTION
 *  Resume stitching panorama process
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *  MDI_RES_CAMERA_ERR_FAILED   Failed
 *****************************************************************************/
MDI_RESULT mdi_camera_resume_stitching_panorama(void)
{
#if defined(__MDI_CAMERA_PANORAMA_VIEW_SUPPORT__)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MDI_RESULT result;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    result = media_cam_resume_panorama(stack_int_get_active_module_id());

    if (result == MED_RES_OK)
    {
        mdi_camera_p->state = CAMERA_STATE_STITCH_PANORAMA;
        return MDI_RES_CAMERA_SUCCEED;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_FAILED;
    }

#else /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
    return MDI_RES_CAMERA_ERR_FAILED;
#endif /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_start_stitching_panorama
 * DESCRIPTION
 *  Start stitching panorama process
 * PARAMETERS
 *  data            [IN]        Panorama capture image information
 *  callback        [IN]        Stitch result callbcak
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *  MDI_RES_CAMERA_ERR_FAILED   Failed
 *****************************************************************************/
MDI_RESULT mdi_camera_start_stitching_panorama( mdi_camera_panorama_struct* data, mdi_camera_panorama_callback callback)
{
#if defined(__MDI_CAMERA_PANORAMA_VIEW_SUPPORT__)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_start_stitch_req_struct panorama_data;
    MDI_RESULT result;
    U8 i = 0;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(mdi_camera_p->state == CAMERA_STATE_IDLE);

    SetProtocolEventHandler(mdi_camera_panorama_result_ind, MSG_ID_MEDIA_CAM_STITCH_RESULT_IND);

    panorama_data.direction = data->direction;
    panorama_data.num_of_jpegs = data->image_num;

    while(i < data->image_num)
    {
        panorama_data.jpeg_buffer_p[i] = data->src_jpeg_buffer_address[i];
        panorama_data.jpeg_buffer_size[i] = data->src_jpeg_buffer_size[i];
        i++;
    }
    
    panorama_data.source_height = camera_setting_p->image_height;
    panorama_data.source_width = camera_setting_p->image_width;

    panorama_data.target_buffer_size = data->dest_jpeg_buffer_size;
    panorama_data.target_buffer_address = data->dest_jpeg_buffer_address;

    panorama_data.seq_no = mdi_camera_p->stitch_seq_num;

    g_mdi_camera_panorama_callback = callback;

    result = media_cam_stitch_panorama(stack_int_get_active_module_id(), &panorama_data);

    mdi_camera_p->state = CAMERA_STATE_STITCH_PANORAMA;

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_FAILED;
    }

#else /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */

    /* not supported on !__MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
    ASSERT(0);    

#endif /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_stop_stitching_panorama
 * DESCRIPTION
 *  Stop stitching panorama process
 * PARAMETERS
 *  void
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *  MDI_RES_CAMERA_ERR_FAILED   Failed
 *****************************************************************************/
MDI_RESULT mdi_camera_stop_stitching_panorama(void)
{
#if defined(__MDI_CAMERA_PANORAMA_VIEW_SUPPORT__)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MDI_RESULT result;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    result = media_cam_abort_panorama(stack_int_get_active_module_id());
   
    mdi_camera_p->stitch_seq_num++;
    mdi_camera_p->state = CAMERA_STATE_IDLE;

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_FAILED;
    }
    
#else /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */

    /* not supported on !__MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
    ASSERT(0);    

#endif /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_panorama_result_ind
 * DESCRIPTION
 *  Panorama finish indication mesg which is from media task.
 * PARAMETERS
 *  inMsg           [IN]        stitch result     
 * RETURNS
 *  void
 *****************************************************************************/
static void mdi_camera_panorama_result_ind(void *inMsg)
{
#if defined(__MDI_CAMERA_PANORAMA_VIEW_SUPPORT__)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    media_cam_stitch_result_ind_struct *msg_p = (media_cam_stitch_result_ind_struct*) inMsg;
    mdi_camera_panorama_result_struct result_ind;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_TRACE(
        MMI_MEDIA_TRC_G4_MDI, 
        MDI_TRC_CAMERA_PANORAMA_STITCH_RESULT, 
        mdi_camera_p->state, 
        mdi_camera_p->stitch_seq_num);
    
    memset(&result_ind,0x0,sizeof(mdi_camera_panorama_result_struct));

    if (mdi_camera_p->stitch_seq_num == msg_p->seq_no)
    {
        if (msg_p->result == MED_RES_OK)
        {
            result_ind.result = MDI_RES_CAMERA_SUCCEED;
        
            result_ind.jpeg_buffer_address = msg_p->jpeg_file_address;
            result_ind.jpeg_buffer_size = msg_p->jpeg_file_size;
            result_ind.jpeg_image_width = msg_p->jpeg_file_width;
            result_ind.jpeg_image_height = msg_p->jpeg_file_height;
        }
        else
        {
            result_ind.result = MDI_RES_CAMERA_ERR_FAILED;
        }

        g_mdi_camera_panorama_callback(&result_ind);

        if (mdi_camera_p->state == CAMERA_STATE_STITCH_PANORAMA)
        {
            mdi_camera_p->state = CAMERA_STATE_IDLE;
        }
    }

#else /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */

    /* not supported on !__MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
    ASSERT(0);    

#endif /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
}


/*****************************************************************************
 * FUNCTION
 *  mdi_camera_reset_panorama_3a
 * DESCRIPTION
 *  Resets 3A when terminating panorama process.
 * PARAMETERS
 *  void     
 * RETURNS
 *  MDI_RES_CAMERA_SUCCEED      Success
 *  MDI_RES_CAMERA_ERR_FAILED   Failed
 *****************************************************************************/
MDI_RESULT mdi_camera_reset_panorama_3a(void)
{
#if defined(__MDI_CAMERA_PANORAMA_VIEW_SUPPORT__)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    MDI_RESULT result;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    result = media_cam_reset_panorama_3a(stack_int_get_active_module_id());

    if (result == MED_RES_OK)
    {
        return MDI_RES_CAMERA_SUCCEED;
    }
    else
    {
        return MDI_RES_CAMERA_ERR_FAILED;
    }
#else /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
    return MDI_RES_CAMERA_ERR_FAILED;
#endif /* __MDI_CAMERA_PANORAMA_VIEW_SUPPORT__ */
}

#ifdef MBM_IMIBASE_SUPPORT
typedef struct
{
    U16 image_width;                    /* Capture image width */
    U16 image_height;                   /* Capture image height */
    U16 preview_width;                  /* Preview image width */
    U16 preview_height;                 /* Preview image height */
    U16 lcm;                            /* LCM */
    U16 preview_rotate;                 /* Preview rotation */
    U16 lcm_rotate;                     /* LCM rotation */
    U8 continue_capture;                /* Continue capture flag */
    void (*xenon_flash_status_callback) (mdi_result);   /* xenon flash status callback function pointer */

    gdi_handle preview_layer_handle;    /* Preview layer handle */
    S32 preview_wnd_offset_x;           /* Preview window offset x */
    S32 preview_wnd_offset_y;           /* Preview window offset y */
    U16 preview_wnd_width;              /* Preview window width */
    U16 preview_wnd_height;             /* Preview window height */
    U32 blt_layer_flag;                 /* Layer flags of needing to blt when camera previewing */
    U32 preview_layer_flag;             /* Previwe layer flag */
    BOOL is_lcd_update;                 /* LCD update the preview layer flag */
    U16 src_key_color;                  /* Source key color */
    BOOL is_tvout;                      /* Update to TV flag */ 
}t_imi_camera_settting;

t_imi_camera_settting g_miipts_imi_camera_setting;                                                                        
mdi_camera_setting_struct g_miipts_camera_setting_data;
mdi_camera_preview_struct g_miipts_camera_preview_data;

void imi_camera_load_setting(mdi_camera_setting_struct* camera_setting_data, mdi_camera_preview_struct* camera_preview_data, t_imi_camera_settting* imi_camera_setting)
{
    camera_setting_data->lcm                          = imi_camera_setting->lcm;
    camera_setting_data->preview_width                = imi_camera_setting->preview_width;
    camera_setting_data->preview_height               = imi_camera_setting->preview_height;
    
    camera_setting_data->preview_rotate               = imi_camera_setting->preview_rotate;
    camera_setting_data->lcm_rotate                   = imi_camera_setting->lcm_rotate;
    camera_setting_data->image_width                  = imi_camera_setting->image_width;
    camera_setting_data->image_height                 = imi_camera_setting->image_height;
    camera_setting_data->continue_capture             = imi_camera_setting->continue_capture;
    camera_setting_data->xenon_flash_status_callback  = imi_camera_setting->xenon_flash_status_callback;
    
    camera_preview_data->preview_layer_handle         = imi_camera_setting->preview_layer_handle;
    camera_preview_data->preview_wnd_offset_x         = imi_camera_setting->preview_wnd_offset_x;
    camera_preview_data->preview_wnd_offset_y         = imi_camera_setting->preview_wnd_offset_y;
    camera_preview_data->preview_wnd_width            = imi_camera_setting->preview_wnd_width;
    camera_preview_data->preview_wnd_height           = imi_camera_setting->preview_wnd_height;
    
    camera_preview_data->blt_layer_flag               = imi_camera_setting->blt_layer_flag;
    camera_preview_data->preview_layer_flag           = imi_camera_setting->preview_layer_flag;
    camera_preview_data->is_lcd_update                = imi_camera_setting->is_lcd_update;
    camera_preview_data->src_key_color                = imi_camera_setting->src_key_color;
    camera_preview_data->is_tvout                     = imi_camera_setting->is_tvout;
}
#endif//MBM_IMIBASE_SUPPORT
#if defined(__MTK_INTERNAL__)
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif /* __MTK_INTERNAL__ */

#endif /* __MMI_CAMERA__ */ 

